@Override
    protected byte[] generateVariableHeader() throws MQTTException, IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        // Some packages has this
        if(protocolName == null)
            out.write(getProtocol());
        else{
            // For some reason I need to test this in the interop testing...
            out.write((protocolName.getBytes("UTF-8").length >> 8) & 0xFF); // MSB
            out.write(protocolName.getBytes("UTF-8").length & 0xFF); // LSB
            out.write(protocolName.getBytes("UTF-8"));

            // Protocol Level (Used by all messages)
            out.write((byte) VERSION_311);
        }

        // CONNECT FLAGS
        byte flags = (byte) (0x00);

        byte username = (byte) ((this.username != null && this.username
                .length() >= 0) ? 0x01 : 0x00);
        byte password = (byte) ((this.password != null && this.password
                .length() >= 0) ? 0x01 : 0x00);
        byte willRetain = (byte) (this.willRetain ? 0x01 : 0x00);
        byte willQoS = this.willQoS;
        byte willFlag = (byte) ((this.willRetain && this.willFlag) ? 0x01
                : 0x00);
        byte cleanSession = (byte) (this.cleanSession ? 0x01 : 0x00);
        byte RESERVED = (byte) 0x00;

        flags = (byte) ((username << 7) | (password << 6) | (willRetain << 5)
                | (willQoS << 3) | (willFlag << 2) | (cleanSession << 1) | (RESERVED << 0));

        // Write flags
        out.write(flags);

        // Keep alive (Use lower two bytes of integer)
        out.write(MQTTHelper.MSB(keepAlive));
        out.write(MQTTHelper.LSB(keepAlive));

        return out.toByteArray();
    }