We are trying to stream screen over network for educational purpose but the received image is not always correct sometimes it sends only some part of it instead of the full picture.

Code:
Console.WriteLine("That program can transfer small file. I've test up to 850kb file");

IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

sock.Bind(ipEnd);

sock.Listen(100);

Socket clientSock = sock.Accept();

byte[] clientData = new byte[1024 * 5000];

string receivedPath = "c:/";

int receivedBytesLen = clientSock.Receive(clientData);

int fileNameLen = BitConverter.ToInt32(clientData, 0);

string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);

Console.WriteLine("Client:{0} connected & File {1} started received.", clientSock.RemoteEndPoint, fileName);


BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Create)); ;

bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);

Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath);

bWrite.Close();

clientSock.Close();

sock.Close();

if (File.Exists("c:\\screen.jpg"))

{

FileStream temp = File.Open(@"c:\\screen.jpg", FileMode.Open);

long filelen = temp.Length;

temp.Close();

if (filelen > 15000)

{

bmpScreenShot = new Bitmap(@"c:\\screen.jpg");

pictureBox1.Image = bmpScreenShot;

}

}