             throw TCPError.unknown(description: "Port was not defined in URI")
         }
         socket = tcpconnect(try IP(remoteAddress: host, port: port).address, never)
+        if let socket = self.socket {
+            if socket == nil {
+                throw TCPError.closedSocket(description: "Unable to connect.")
+            }
+            else {
+                closed = false
+            }
+        }
     }
-    
-    
-    public func receive(max byteCount: Int) throws -> C7.Data {
-        return try receive(upTo: byteCount, timingOut: never)
+
+    public func send(data: C7.Data) throws {
+        try send(data, flushing: false, deadline: never)
     }
-    
-    public func send(data: C7.Data) throws {
-        try send(data, flushing: true, deadline: never)
-    }
-    
+
     public func flush() throws {
         try flush(timingOut: never)
     }

    public func send(data: Data, flushing flush: Bool = true, deadline: Deadline = never) throws {
        let socket = try getSocket()
        try assertNotClosed()
        
        let bytesProcessed = data.withUnsafeBufferPointer {
            tcpsend(socket, $0.baseAddress, $0.count, deadline)
        }

        try TCPError.assertNoSendErrorWithData(data, bytesProcessed: bytesProcessed)

        if flush {
            try self.flush()
        }
    }

    public func receive(upTo byteCount: Int, timingOut deadline: Deadline = never) throws -> Data {
        let socket = try getSocket()
        try assertNotClosed()

        var data = Data.bufferWithSize(byteCount)
        let bytesProcessed = data.withUnsafeMutableBufferPointer {
            tcprecv(socket, $0.baseAddress, $0.count, deadline)
        }

        try TCPError.assertNoReceiveErrorWithData(data, bytesProcessed: bytesProcessed)
        return Data(data.prefix(bytesProcessed))
    }

        let result = try receive(upTo: codeUnitCount, until: delimiter, timingOut: deadline)
        return try String(data: result)
    }
}
