private void ReadCallback(IAsyncResult ar)
{
StateObject state = (StateObject) ar.AsyncState;
Socket socket = state.WorkSocket;
// Read data from the client socket.
int bytesRead = socket.EndReceive(ar);
if (bytesRead > 0)
{
bool endOfTransmission = ProcessIncomingStream(bytesRead, state);
// Check for end-of-file tag. If it is not there, read
// more data.
if (!endOfTransmission)
{
// Not all data received. Get more.
socket.BeginReceive(state.Buffer, 0, state.BufferSize, 0, ReadCallback, state);
}
}
// Will simply shutdown communication if nothing received
}