         fatalError("this must be overridden by sub class")
     }
 
-    /// Flush data to the underlying socket.
+    /// Flush data to the underlying socket and return if this socket needs to be registered for write notifications.
     ///
-    /// - returns: `true` if there is no further data to write.
-    fileprivate func flushNow() -> Bool {
+    /// - returns: If this socket should be registered for write notifications. Ie. `IONotificationState.register` if _not_ all data could be written, so notifications are necessary; and `IONotificationState.unregister` if everything was written and we don't need to be notified about writability at the moment.
+    fileprivate func flushNow() -> IONotificationState {
         fatalError("this must be overridden by sub class")
     }
 

        }
    }

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

        defer {
            inFlushNow = false
        }

        inFlushNow = true

        while !closed {
            do {
                switch try self.writeToSocket(pendingWrites: pendingWrites) {
                case .couldNotWriteEverything:
                    return false
                case .writtenCompletely:
                    return true
                }
            } 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 true
            }
        }
        return true
    }
}


class BaseSocketChannel<T : BaseSocket> : SelectableChannel, ChannelCore {
    typealias SelectableType = T

    /// Indicates if a selectable should registered or not for IO notifications.
    enum IONotificationState {
        /// We should be registered for IO notifications.
        case register

        /// We should not be registered for IO notifications.
        case unregister
    }

    // MARK: Methods to override in subclasses.

    /// `true` if the whole `Channel` is closed and so no more IO operation can be done.
