         // Non-blocking, dispatch connection, check errno for connection error.
         let error = Error(rawValue: errno)
         if case Error.inProgress = error {
-            
+            // Wait for channel to be writable. Then we are connected.
             dispatch_io_write(channel, off_t(), dispatch_data_empty, dispatch_get_main_queue()) { done, data, error in
-                log.debug("Writing to see if open, done: \(done), data: \(data), error: \(Error(rawValue: error))")
-            }
-            
-            // Wait for source to be writable. Then we are connected.
-            dispatch_source_set_event_handler(connectingSource) { [connectingSource = self.connectingSource] in
-                log.debug("Writing hap")
                 var result = 0
                 var resultLength = socklen_t(strideof(result.dynamicType))
                 let ret = getsockopt(self.fd.rawValue, SOL_SOCKET, SO_ERROR, &result, &resultLength)

        self.loop = loop
        self.socketFD = fd
        
        // Create the dispatch source for listening
        self.connectingSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_WRITE, UInt(fd.rawValue), 0, dispatch_get_main_queue())
        self.channel = dispatch_io_create(DISPATCH_IO_STREAM, fd.rawValue, dispatch_get_main_queue()) { error in
            if error != 0 {
                try! { throw Error(rawValue: error) }()
            }
        }
        
        // Set SO_REUSEADDR
        var reuseAddr = 1
        let error = setsockopt(self.fd.rawValue, SOL_SOCKET, SO_REUSEADDR, &reuseAddr, socklen_t(strideof(Int)))
        if error != 0 {
            try! { throw Error(rawValue: error) }()
        }
    }
    

                if result != 0 {
                    try! { throw Error(rawValue: Int32(result)) }()
                }
                log.debug("Connection established on \(self.fd)")
                onConnect()
            }
        } else {
            throw error
        }
    }
    
    func close () {
        dispatch_io_close(channel, 0)
    }
}