          * |----------------------------------------------------------------------------------
          */
 
-        let connectBytes: [UInt8] = [
-            0x10,                               // FIXED BYTE 1   1 = CONNECT, 0 = DUP QoS RETAIN, not used in CONNECT
-            UInt8(client.utf8.count + 12),      // FIXED BYTE 2   remaining length, client id length + 12
+        var remainingLength:Int = 10 // initial 10 bytes
+        remainingLength += (2 + client.utf8.count) // 2 byte client id length + codepoints
+        var connectFlags:UInt8 = 0b00000010 // clean session
+        if let username = self.username {
+            connectFlags |= 0b10000000
+            remainingLength += (2 + username.utf8.count) // 2 byte username length + codepoints
+        }
+        if let password = self.password {
+            connectFlags |= 0b01000000
+            remainingLength += (2 + password.utf8.count) // 2 byte password length + codepoints
+        }
+        let remainingLengthBytes = encodeVariableLength(UInt(remainingLength))
+
+        let headerBytes: [UInt8] = [
+            0x10] +                             // FIXED BYTE 1   1 = CONNECT, 0 = DUP QoS RETAIN, not used in CONNECT
+         remainingLengthBytes +                 // FIXED BYTE 2+   remaining length
+         [
             0x00,                               // VARIA BYTE 1   length MSB
             0x04,                               // VARIA BYTE 2   length LSB is 4
             0x4d,                               // VARIA BYTE 3   M

            0x04,                               // VARIA BYTE 7   Version = 4
            0x02,                               // VARIA BYTE 8   Username Password RETAIN QoS Will Clean flags
            keepalive.highByte,                 // VARIA BYTE 9   Keep Alive MSB
            keepalive.lowByte,                  // VARIA BYTE 10  Keep Alive LSB
            UInt16(client.utf8.count).highByte, // VARIA BYTE 11  client id length MSB
            UInt16(client.utf8.count).lowByte   // VARIA BYTE 12  client id length LSB
        ]

        let messageBytes = connectBytes + [UInt8](client.utf8)
        outputStream?.write(messageBytes, maxLength: messageBytes.count)
    }



    private var pingInterval: UInt16 = 10
    private var useTLS = false
    private var username:String?
    private var password:String?

    private var inputStream: InputStream?
    private var outputStream: OutputStream?
