private void Begin_Receive(IAsyncResult ar) { if (ClientSocket != null) { try { ClientSocket.EndReceive(ar); byte[] Header = lengthBUFF; int length = BitConverter.ToInt32(Header, 0); byte[] Payload = ReceiveData(length, ClientSocket); if (Header.Length == 4 && Payload.Length == length) { Data DTU = Data.Desserialize(Payload); if (DTU.type != 0) { if (DTU.type == 1) { Rectangle bounds = new Rectangle(DTU.bx, DTU.by, DTU.bwidth, DTU.bheight); Image restoreIMG; if (DTU.comp) { restoreIMG = LZ4mm.LZ4Codec.Decode32(DTU.dataBytes, 0, DTU.dataBytes.Length, DTU.dataSize) .toBitmap(); } else { restoreIMG = (Bitmap)DTU.dataBytes.toBitmap(); } if (!firstImage) { oldImage = (Bitmap)restoreIMG; firstImage = true; } else { Utils.UpdateScreen(ref oldImage, restoreIMG, bounds); } } else if (DTU.type == 2) { oldImage = (Bitmap)DTU.dataBytes.toBitmap(); } this.pictureBox1.Image = oldImage; FPS++; if (fpsCounter.ElapsedMilliseconds >= 1000) { this.Invoke(new MethodInvoker(() => { this.lblFPS.Text = FPS.ToString(); })); FPS = 0; fpsCounter = Stopwatch.StartNew(); } } } } catch (Exception ex) { //MessageBox.Show("Exception: " + ex.Message); } try { if (ClientSocket.Connected) { ClientSocket.BeginReceive(lengthBUFF, 0, lengthBUFF.Length, SocketFlags.None, Begin_Receive, null); } } catch (Exception ex) { Connect(false); //MessageBox.Show("Exception: " + ex.Message); } } }