                                            configuration.port,
                                            &readStream,
                                            &writeStream)
-        
-        input = readStream?.takeRetainedValue()
-        output = writeStream?.takeRetainedValue()
-        streamQueue.async { [weak self] in
-            
-            self?.configureAndOpen(stream: self?.input)
-            self?.configureAndOpen(stream: self?.output)
-            RunLoop.current.run()
-        }
-        streamQueue.async { [weak self] in
-            self?.startTimeoutTimer()
-        }
-    }
-    
-    private func configureAndOpen(stream: Stream?) {
-        
-        stream?.delegate = streamsDelegate
-        stream?.schedule(in: RunLoop.current, forMode: .defaultRunLoopMode)
-        stream?.open()
-    }
-    
+        guard let inputStream: InputStream = readStream?.takeRetainedValue(),
+            let outputStream: OutputStream = writeStream?.takeRetainedValue() else {
+            state = .error(.unknown)
+            return
+        }
+        pthread_rwlock_wrlock(&streamLock)
+        input = inputStream
+        output = outputStream
+        inputStream.delegate = self.streamsDelegate
+        outputStream.delegate = self.streamsDelegate
+        CFReadStreamSetDispatchQueue(input, streamQueue)
+        CFWriteStreamSetDispatchQueue(output, streamQueue)
+        outputStream.open()
+        outputStream.open()
+        pthread_rwlock_wrlock(&streamLock)
+    }
+
     // MARK: - Close -
     open func close() {
         guard state != .closed else { return }

    
    // MARK: - Open -
    open func open() {
        guard state == .closed || state == .error(.unknown) else {
            assertionFailure("Trying to open connection while it already is opened")
            return
        }

        state = .closed
    }
    
    private func flushConnection() {
        
        pthread_rwlock_wrlock(&requestLock)
        invalidateRequestTimeoutTimer()
        requestCompletion = nil
        requestResponse = ""
        pthread_rwlock_unlock(&requestLock)
        pthread_rwlock_wrlock(&streamLock)
        if let input = input {
            
            CFReadStreamSetDispatchQueue(input, nil)
            input.delegate = nil
            input.close()
        }
        if let output = output {
            
            CFWriteStreamSetDispatchQueue(output, nil)
            output.delegate = nil
            output.close()
        }
        input = nil
        output = nil
        pthread_rwlock_wrlock(&streamLock)
    }
    
    // MARK: - Data transmitting -
