public byte[] Encode()
        {
            var packet = "313f";
            var s      = Convert.ToString((int)Type, 2);

            if (s.Length != 3)
            {
                s = s.PadLeft(3 - s.Length + 1, '0');
            }
            var iii = 0;

            foreach (var fl in Flags)
            {
                iii += (int)fl;
            }
            var s1 = Convert.ToString(iii, 2);

            if (s1.Length != 5)
            {
                s1 = s1.PadLeft(5 - s1.Length + 1, '0');
            }
            var typenflags = s1 + s;

            int[] bits = typenflags.PadLeft(8, '0').Select(c => int.Parse(c.ToString())).ToArray();
            var   b    = bits.ToBitArray().ToHex();

            packet += b;    //type and flags
            packet += "00"; //sessionid
            if (Type != PacketTypes.DATA)
            {
                packet += ConnectionSignature; //connection signature
            }
            else
            {
                packet += "5790aecd"; //cdae9057
            }
            packet += "00";           // "db44870f";
            packet += "0001";         //packet number
            //packet += ConnectionSignature; //encrypted empty payload, static//my head hurts....
            packet += "db44870f";
            if (!string.IsNullOrEmpty(Payload))
            {
                packet += "00";
                Payload = PythonScript.CompressPacketPayload(RC4.Encrypt(Encoding.ASCII.GetBytes("CD&ML"), Payload.FromHex()).ToHex()).Result;
                packet += "02";
            }
            packet += Payload;
            packet += CalculateChecksum(packet); //calculate checksum to append at the end
            return(packet.FromHex());
        }