private int GetPayloadLength(IBufferStream bufferStream, int length) { if (length == 2) { var flag = bufferStream.ReadByte(); m_Final = (FINAL_FLAG & flag) == flag; m_OpCode = (sbyte)(flag & 0x08); // one byte playload length var playloadLen = (int)bufferStream.ReadByte(); //the highest bit is mask indicator m_Masked = playloadLen > 128; // remove the mask byte playloadLen = playloadLen % 128; // no extend playload length if (playloadLen < 126) { if (!m_Masked) { return(playloadLen); } // masking-key: 4 bytes return(playloadLen + 4); } // playload length takes 2 bytes if (playloadLen == 126) { ResetSize(4); return(-1); } else// playload length takes 8 bytes { ResetSize(10); return(-1); } } else if (length == 4) { bufferStream.Skip(2); // 2 bytes var playloadLen = bufferStream.ReadUInt16(); if (m_Masked) // add mask key's length { playloadLen += 4; } return(playloadLen); } else // length = 8 { bufferStream.Skip(2); // 8 bytes var playloadLen = bufferStream.ReadUInt64(); if (m_Masked) // add mask key's length { playloadLen += 4; } return((int)playloadLen); } }