         ]
 
         let messageBytes = headerBytes + encode(string: topic) + requestedQosByte
-        outputStream?.write(messageBytes, maxLength: messageBytes.count)
-    }
-
-    private func mqttUnsubscribe(from topic: String) {
+        output.write(messageBytes, maxLength: messageBytes.count)
+    }
+
+    private func mqttUnsubscribe(output: OutputStream, from topic: String) {
         messageId += 1
 
         var remainingLength: Int = 2 // initial 2 bytes (messageID)

                                outputStream: &outputStream)

        guard let input = inputStream, let output = outputStream else {
            return nil
        }

        if options.useTLS {

        remainingLengthBytes                    // remaining length, variable

        let messageBytes = headerBytes + encode(string: topic) + [UInt8](message)
        output.write(messageBytes, maxLength: messageBytes.count)
    }

    @objc private func mqttPing(output: OutputStream) {
        let messageBytes: [UInt8] = [
            0xc0,                               // FIXED BYTE 1   c = PINGREQ, 0 = DUP QoS RETAIN (not used)
            0x00                                // FIXED BYTE 2   remaining length = 0
        ]
        
        output.write(messageBytes, maxLength: messageBytes.count)
    }
    
    // MARK: - Utils
