This doesn\'t always happen. But it happens more often than receiving the bytes completely.
This is how my client prog sends bytes to my server prog:
pub
If Available is zero this does not mean that you have finished reading bytes. It means that currently, in this nanosecond, there are no bytes queued. How could the server know how many bytes will be coming in the future?
In good approximation, every singe use of Available is an error!
Delete all usages of Available. Instead, always try to read a full buffer. If you get 0 this means the socket has been closed and you are done.
Edit: Here is some canonical read code:
var buffer = new byte[8192];
while(true) {
var readCount = stream.Read(buffer, 0, buffer.Length);
if (readCount == 0) break;
outputStream.Write(buffer, 0, readCount);
}