             case Stream.Event.errorOccurred: handleErrorState(inStream: stream)
             case Stream.Event.hasBytesAvailable: handleBytesAvailable(inStream: stream)
             case Stream.Event.hasSpaceAvailable: return
-            case Stream.Event.endEncountered: close(withError: .connectionDidEnd)
+            case Stream.Event.endEncountered: handle(connectionError: .connectionDidEnd)
             default: return
         }
     }
     
     private func checkOpenCompleted() {
         guard state != .open else { return }
-        guard (input?.streamStatus == .open || input?.streamStatus == .reading) &&
-            output?.streamStatus == .open else { return }
+        guard let input = input, let output = output else { return }
+        guard (input.streamStatus == .open || input.streamStatus == .reading) &&
+            output.streamStatus == .open else { return }
         
         state = .open
     }

            return
        }
        streamQueue.async { [weak self] in
            
            self?.state = .transmitting
            self?.requestResponse = ""
            guard data.withUnsafeBytes({ output.write($0, maxLength: data.count) }) == data.count else {
                
                self?.state = .open
                self?.completionQueue.async { completion?(.failure(.sendingDidFail)) }
                return
            }
            self?.requestCompletion = completion
            self?.startTimeoutTimer()
        }
    }


                                           configuration.port,
                                           &readStream,
                                           &writeStream)
        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 }
