     }
     
     ///Create an error
-    func errorWithDetail(detail: String, code: UInt16) -> NSError {
+    private func errorWithDetail(detail: String, code: UInt16) -> NSError {
         var details = Dictionary<String,String>()
         details[NSLocalizedDescriptionKey] =  detail
         return NSError(domain: "Websocket", code: Int(code), userInfo: details)
     }
     
     ///write a an error to the socket
-    func writeError(code: UInt16) {
+    private func writeError(code: UInt16) {
         let buf = NSMutableData(capacity: sizeof(UInt16))
         var buffer = UnsafeMutablePointer<UInt16>(buf.bytes)
         buffer[0] = code.byteSwapped
         dequeueWrite(NSData(bytes: buffer, length: sizeof(UInt16)), code: .ConnectionClose)
     }
     ///used to write things to the stream in a
-    func dequeueWrite(data: NSData, code: OpCode) {
+    private func dequeueWrite(data: NSData, code: OpCode) {
         if _writeQueue == nil {
             _writeQueue = NSOperationQueue()
             _writeQueue!.maxConcurrentOperationCount = 1
         }
         _writeQueue!.addOperationWithBlock {
+            //stream isn't ready, let's wait
+            var tries = 0;
+            while self._outputStream == nil {
+                if(tries < 5) {
+                    sleep(1);
+                } else {
+                    break;
+                }
+                tries++;
+            }
             var offset = 2
             UINT16_MAX
             let bytes = UnsafeMutablePointer<UInt8>(data.bytes)

        }
    }
    ///Finds the HTTP Packet in the TCP stream, by looking for the CRLF.
    func processHTTP(buffer: UnsafePointer<UInt8>, bufferLen: Int) -> Bool {
        let CRLFBytes = [UInt8("\r"), UInt8("\n"), UInt8("\r"), UInt8("\n")]
        var k = 0
        var totalSize = 0

        }
    }
    ///dequeue the incoming input so it is processed in order
    private func dequeueInput() {
        if _inputQueue.count > 0 {
            let data = _inputQueue[0]
            var work = data
