private byte[] EncodeServerData(string text)
{
byte[] bytesRaw = Encoding.GetBytes(text);
byte[] header;
if (bytesRaw.Length <= 125)
{
header = new byte[]
{
129,
(byte) bytesRaw.Length
};
}
else if (bytesRaw.Length >= 126 && bytesRaw.Length <= 65535)
{
header = new byte[]
{
129,
126,
(byte) ((bytesRaw.Length >> 8) & 255),
(byte) (bytesRaw.Length & 255),
};
}
else
{
header = new byte[]
{
129,
127,
(byte) ((bytesRaw.Length >> 56) & 255),
(byte) ((bytesRaw.Length >> 48) & 255),
(byte) ((bytesRaw.Length >> 40) & 255),
(byte) ((bytesRaw.Length >> 32) & 255),
(byte) ((bytesRaw.Length >> 24) & 255),
(byte) ((bytesRaw.Length >> 16) & 255),
(byte) ((bytesRaw.Length >> 8) & 255),
(byte) (bytesRaw.Length & 255)
};
}
var result = new byte[header.Length + bytesRaw.Length];
header.CopyTo(result, 0);
Array.ConstrainedCopy(bytesRaw, 0, result, header.Length, bytesRaw.Length);
return result;
}