         inStream.open()
         outStream.open()
 
-        self.mutex.lock()
+        self.readyToWriteMutex.lock()
         self.readyToWrite = true
-        self.mutex.unlock()
+        self.readyToWriteMutex.unlock()
         
         let bytes = UnsafeRawPointer((data as NSData).bytes).assumingMemoryBound(to: UInt8.self)
         var out = timeout * 1_000_000 // wait 5 seconds before giving up

            writeQueue.cancelAllOperations()
        }
        cleanupStream()
        connected = false
        if runDelegate {
            doDisconnect(error)
        }

    private var didDisconnect = false
    private var readyToWrite = false
    private var headerSecKey = ""
    private let readyToWriteMutex = NSLock()
    private let notificationCenter = NotificationCenter.default
    private var canDispatch: Bool {
        readyToWriteMutex.lock()
        let canWork = readyToWrite
        readyToWriteMutex.unlock()
        return canWork
    }
    /// The shared processing queue used for all WebSocket.
