NetworkStream cuts off first 4 bytes when reading
<p>I ran into a strange problem. When I'm trying to send the file via a TCP socket, the first 4 bytes of sended information cuts off.</p>

<p>That is sending and receives pieces of code.</p>

<p>Client side</p>

<pre><code>for (var i = 0; i &lt; fileContentByte.Length; i += buffer.Length)
{
    var size = (i + buffer.Length &gt; fileContentByte.Length) ? fileContentByte.Length - i : buffer.Length;
    clientSocket.Write(fileContentByte, i, size);
}
</code></pre>

<p>Server side</p>

<pre><code>using(var file = File.Create("C:\\test\\"+fileName.Substring(0, fileName.IndexOf('\0'))))
while(bytesReceived &lt; numberOfBytes &amp;&amp; (count = clientStream.Read(buffer, 0, buffer.Length)) &gt; 0)
{
    file.Write(buffer, 0, count);
    bytesReceived += count;
}
</code></pre>

<p>Here is link on full code - <a href="http://pastebin.com/VwTgTxgb" rel="nofollow">http://pastebin.com/VwTgTxgb</a></p>