public IPacket Read(SocketClient socket)
        {
            try {
                bool fin = (socket.Buffer[0] & 0x80) == 0x80;

                bool rsv1 = (socket.Buffer[0] & 0x40) == 0x40;
                bool rsv2 = (socket.Buffer[0] & 0x20) == 0x20;
                bool rsv3 = (socket.Buffer[0] & 0x10) == 0x10;

                OpCode opCode = (OpCode)((socket.Buffer[0] & 0x8) | (socket.Buffer[0] & 0x4) | (socket.Buffer[0] & 0x2) | (socket.Buffer[0] & 0x1));

                bool mask = (socket.Buffer[1] & 0x80) == 0x80;

                byte  payload = (byte)((socket.Buffer[1] & 0x40) | (socket.Buffer[1] & 0x20) | (socket.Buffer[1] & 0x10) | (socket.Buffer[1] & 0x8) | (socket.Buffer[1] & 0x4) | (socket.Buffer[1] & 0x2) | (socket.Buffer[1] & 0x1));
                ulong length  = 0;

                switch (payload)
                {
                case 126:
                    byte[] bytesUShort = socket.Receive(2);
                    if (bytesUShort != null)
                    {
                        length = BitConverter.ToUInt16(bytesUShort.Reverse().ToArray(), 0);
                    }
                    break;

                case 127:
                    byte[] bytesULong = socket.Receive(8);
                    if (bytesULong != null)
                    {
                        length = BitConverter.ToUInt16(bytesULong.Reverse().ToArray(), 0);
                    }
                    break;

                default:
                    length = payload;
                    break;
                }

                byte[] maskKeys = null;
                if (mask)
                {
                    maskKeys = socket.Receive(4);
                }

                WSPacket packet = null;

                if (length > 0)
                {
                    byte[] data = socket.Receive((int)length);

                    if (mask)
                    {
                        for (int i = 0; i < data.Length; ++i)
                        {
                            data[i] = (byte)(data[i] ^ maskKeys[i % 4]);
                        }
                    }

                    ushort closeCode = 0;
                    if (opCode == OpCode.Close && data.Length == 2)
                    {
                        closeCode = BitConverter.ToUInt16(((byte[])data.Clone()).Reverse().ToArray(), 0);
                    }

                    if (closeCode != 0)
                    {
                        packet = new WSPacket(data, opCode, closeCode);
                    }
                    else
                    {
                        packet = new WSPacket(data, opCode);
                    }
                }
                else
                {
                    packet = new WSPacket(null, opCode);
                }

                return(packet);
            } catch (Exception ex) {
                Log.Add("websocket transport protocol Read exception: " + ex.ToString());
            }

            return(null);
        }
        public IPacket Read(SocketClient socket)
        {
            try {
                bool fin = (socket.Buffer[0] & 0x80) == 0x80;

                bool rsv1 = (socket.Buffer[0] & 0x40) == 0x40;
                bool rsv2 = (socket.Buffer[0] & 0x20) == 0x20;
                bool rsv3 = (socket.Buffer[0] & 0x10) == 0x10;

                OpCode opCode = (OpCode)((socket.Buffer[0] & 0x8) | (socket.Buffer[0] & 0x4) | (socket.Buffer[0] & 0x2) | (socket.Buffer[0] & 0x1));

                bool mask = (socket.Buffer[1] & 0x80) == 0x80;

                byte payload = (byte)((socket.Buffer[1] & 0x40) | (socket.Buffer[1] & 0x20) | (socket.Buffer[1] & 0x10) | (socket.Buffer[1] & 0x8) | (socket.Buffer[1] & 0x4) | (socket.Buffer[1] & 0x2) | (socket.Buffer[1] & 0x1));
                ulong length = 0;

                switch(payload) {
                    case 126:
                        byte[] bytesUShort = socket.Receive(2);
                        if(bytesUShort != null) {
                            length = BitConverter.ToUInt16(bytesUShort.Reverse().ToArray(), 0);
                        }
                        break;
                    case 127:
                        byte[] bytesULong = socket.Receive(8);
                        if(bytesULong != null) {
                            length = BitConverter.ToUInt16(bytesULong.Reverse().ToArray(), 0);
                        }
                        break;
                    default:
                        length = payload;
                        break;
                }

                byte[] maskKeys = null;
                if(mask) {
                    maskKeys = socket.Receive(4);
                }

                WSPacket packet = null;

                if(length > 0) {
                    byte[] data = socket.Receive((int)length);

                    if(mask) {
                        for(int i = 0; i < data.Length; ++i) {
                            data[i] = (byte)(data[i] ^ maskKeys[i % 4]);
                        }
                    }

                    ushort closeCode = 0;
                    if(opCode == OpCode.Close && data.Length == 2) {
                        closeCode = BitConverter.ToUInt16(((byte[])data.Clone()).Reverse().ToArray(), 0);
                    }

                    if(closeCode != 0) {
                        packet = new WSPacket(data, opCode, closeCode);
                    } else {
                        packet = new WSPacket(data, opCode);
                    }
                } else {
                    packet = new WSPacket(null, opCode);
                }

                return packet;

            } catch(Exception ex) {
                Log.Add("websocket transport protocol Read exception: " + ex.ToString());
            }

            return null;
        }