 
     private var inputStream: InputStream?
     private var outputStream: OutputStream?
+    private var writeQueue = DispatchQueue(label: "mqtt_write")
 
     private var messageId: UInt16 = 0
 


    private func readStream(input: InputStream, output: OutputStream) {
        var messageParserState: MQTTMessageParserState = .decodingHeader
        var messageType: MQTTMessage = .connack

        var messageLengthMultiplier = 1
        var messageLength = 0


import Foundation


fileprivate enum PacketType: UInt8 {
    // http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718021
    case connect =     0x10
    case connack =     0x20
    case publish =     0x30
    case puback =      0x40
    case pubrec =      0x50
    case pubrel =      0x60
    case pubcomp =     0x70
    case subscribe =   0x80
    case suback =      0x90
    case unsubscribe = 0xa0
    case unsuback =    0xb0
    case pingreq =     0xc0
    case pingresp =    0xd0
    case disconnect =  0xe0
}


fileprivate enum PacketFlags: UInt8 {
    // http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718022
    case dup =           0b0000_1000
    case mostOnce =      0b0000_0000
    case leastOnce =     0b0000_0010
    case justOnce =      0b0000_0100
    case retain =        0b0000_0001
}


final class LightMQTT {

    enum MQTTMessageParserState {
        case decodingHeader
