public static byte[] Encode(GoIMMessage message)
        {
            byte[]       result = new Byte[message.packageLength];
            MemoryStream stream = new MemoryStream(result);

            stream.Write(ToBE(BitConverter.GetBytes(message.packageLength)), WS_PACKAGE_OFFSET - (int)stream.Position, 4);
            stream.Write(ToBE(BitConverter.GetBytes(message.headerLength)), WS_HEADER_OFFSET - (int)stream.Position, 2);
            stream.Write(ToBE(BitConverter.GetBytes(message.protocolVersion)), WS_VERSION_OFFSET - (int)stream.Position, 2);
            stream.Write(ToBE(BitConverter.GetBytes(message.operation)), WS_OPERATION_OFFSET - (int)stream.Position, 4);
            stream.Write(ToBE(BitConverter.GetBytes(message.sequenceId)), WS_SEQUENCE_OFFSET - (int)stream.Position, 4);
            stream.Write(message.Body, message.headerLength - (int)stream.Position, message.Body.Length);
            return(result);
        }
 protected override void OnMessage(MessageEventArgs e)
 {
     Program.log("INFO", userpoint + " Received " + e.RawData.Length + " Bytes from Local");
     byte[][] packets = GoIMProtocol.Cut(e.RawData);
     Program.log("INFO", userpoint + " Received " + packets.Length + " Packets from Local");
     foreach (byte[] packet in packets)
     {
         GoIMMessage data = GoIMProtocol.Decode(e.RawData);
         if (data.operation == GoIMProtocol.WS_OP_USER_AUTHENTICATION)
         {
             var authInfo = JObject.Parse(Encoding.UTF8.GetString(data.Body));
             if (!authInfo.ContainsKey("protover"))
             {
                 authInfo.Add("protover", 2);
                 data.Body = Encoding.UTF8.GetBytes(authInfo.ToString());
             }
             else if ((int)authInfo["protover"] < 2)
             {
                 authInfo["protover"] = 2;
                 data.Body            = Encoding.UTF8.GetBytes(authInfo.ToString());
             }
             if (connected)
             {
                 var newpacket = GoIMProtocol.Encode(data);
                 Program.log("INFO", userpoint + " Sending " + newpacket.Length + " Bytes to Remote.");
                 ws.Send(newpacket);
             }
             else
             {
                 cache.Add(GoIMProtocol.Encode(data));
             }
         }
         else
         {
             if (connected)
             {
                 Program.log("INFO", userpoint + " Sending " + packet.Length + " Bytes to Remote.");
                 ws.Send(packet);
             }
             else
             {
                 cache.Add(packet);
             }
         }
     }
 }
 private void packetHandler(Byte[] bytes)
 {
     Byte[][] packets = GoIMProtocol.Cut(bytes);
     Program.log("INFO", userpoint + " Received " + packets.Length + " Packets from Remote");
     foreach (Byte[] packet in packets)
     {
         GoIMMessage data = GoIMProtocol.Decode(packet);
         if (data.operation == GoIMProtocol.WS_OP_MESSAGE && data.protocolVersion == 2)
         {
             Program.log("INFO", userpoint + " Received Compressed Packets from Remote");
             var           rawStream     = new MemoryStream(data.Body, 2, data.Body.Length - 2);
             DeflateStream deflateStream = new DeflateStream(rawStream, CompressionMode.Decompress, false);
             MemoryStream  memoryStream  = new MemoryStream();
             deflateStream.CopyTo(memoryStream);
             packetHandler(memoryStream.ToArray());
         }
         else
         {
             Program.log("INFO", userpoint + " Sending " + packet.Length + " Bytes to Local.");
             Send(packet);
         }
     }
 }