         assert(!closed)
 
         finishConnect()  // If we were connecting, that has finished.
-        if flushNow() {
+        if flushNow() == .unregister {
             // Everything was written or connect was complete
             finishWritable()
         }

        fatalError("this must be overridden by sub class")
    }

    /// Flush data to the underlying socket.
    ///
    /// - returns: `true` if there is no further data to write.
    fileprivate func flushNow() -> Bool {
        fatalError("this must be overridden by sub class")
    }


        self.pendingWrites.failAll(error: error, close: true)
    }

    override fileprivate func flushNow() -> IONotificationState {
        // Guard against re-entry as data that will be put into `pendingWrites` will just be picked up by
        // `writeToSocket`.
        guard !inFlushNow && !closed else {
            return .unregister
        }

        defer {
            inFlushNow = false
        }
        inFlushNow = true

        do {
            switch try self.writeToSocket(pendingWrites: pendingWrites) {
            case .couldNotWriteEverything:
                return .register
            case .writtenCompletely:
                return .unregister
            }
        } catch let err {
            // If there is a write error we should try drain the inbound before closing the socket as there may be some data pending.
            // We ignore any error that is thrown as we will use the original err to close the channel and notify the user.
            if readIfNeeded0() {

                // We need to continue reading until there is nothing more to be read from the socket as we will not have another chance to drain it.
                while let read = try? readFromSocket(), read == .some {
                    pipeline.fireChannelReadComplete()
                }
            }

            close0(error: err, mode: .all, promise: nil)

            // we handled all writes
            return .unregister
        }
    }

    private func writeToSocket(pendingWrites: PendingDatagramWritesManager) throws -> OverallWriteResult {
