public static int WriteString(this Stream s, Encoding encoding, string text)
{
byte[] bytes;
if (!string.IsNullOrEmpty(text))
bytes = encoding.GetBytes(text);
else
bytes = new byte[0];
s.Write(bytes.Length);
s.Write(bytes);
return 4 + bytes.Length;
}