         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

    private var outputStream: OutputStream?
    private var connected = false
    private var isConnecting = false
    private var writeQueue = OperationQueue()
    private var readStack = [WSResponse]()
    private var inputQueue = [Data]()

    private var certValidated = false
    private var didDisconnect = false
    private var readyToWrite = false
    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.
