title: How can I improve send/receive speed using TCP? C#
body: <p>I'm making a Remote Desktop application using TCP, I've tried/searched for many ways to capture the screen and send it but they all do the same thing.</p>
  <p><strong>To Send:</strong></p>
  <ol>
  <li>Capture the screen using Bitmap and the copyfrom method</li>
  <li>Use memorystream to save the bitmap</li>
  <li>Use TCP socket to send the bitmap serialized</li>
  </ol>
  <p><strong>To Recive:</strong></p>
  <ol>
  <li>Receive the message with readbytes method</li>
  <li>Use memorystream to store the byte array</li>
  <li>Use <code>Image.FromStream(memorystream)</code> to create a image</li>
  </ol>
  <p>It works nice on LAN connection but when I connect with a remote server using VPN, the image takes 0.5 to 5 seconds to arrive</p>
  <p>this is my code:</p>
  <p><strong>DeskTop Class:</strong></p>
  <pre><code>internal static class Desktop
  {
      public static Image TakeScreenShoot()
      {
          Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
          try
          {
              using (Graphics Graphics = Graphics.FromImage(bitmap))
              {
                  Graphics.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
              }
          }
          catch
          {
              bitmap = null;
          }
          return bitmap;
      }
  
      public static void SerializeScreen(Stream stream, Image Image)
      {
          MemoryStream memory = new MemoryStream();
          Image.Save(memory, System.Drawing.Imaging.ImageFormat.Jpeg);
          int numBytes = (int)memory.Length;
          BinaryWriter binaryWriter = new BinaryWriter(stream);
          binaryWriter.Write(numBytes);
          binaryWriter.Write(memory.GetBuffer(), 0, numBytes);
          stream.Flush()
      }
  
      public static Image DeserializeScreen(Stream stream)
      {
          BinaryReader binaryReader = new BinaryReader(stream);
          int numBytes = binaryReader.ReadInt32();
          byte[] buffer = binaryReader.ReadBytes(numBytes);
          MemoryStream memory = new MemoryStream(buffer);
          return Image.FromStream(memory);
      }
  }
  </code></pre>
  <p><strong>Host class</strong></p>
  <pre><code>private void SendImage()
      {
          while (Status == ServerStatus.Connected)
          {
              try
              {
                  Image bitmap = Desktop.TakeScreenShoot();
                  Desktop.SerializeScreen(_NetStream,bitmap);
              }
              catch
              {
                  
              }
          }
      }
  </code></pre>
  <p><strong>Client Class</strong></p>
  <pre><code>protected void ReciveMessage()
      {
       while(Status == ServerStatus.Connected)
        {
          try
          {
              ImageRecibed?.Invoke(Desktop.DeserializeScreen(_NetStream));
          }
          catch
          {
  
          }
        }
      }
  </code></pre>
  <p>How can I improve my code to run faster?</p>
  <p>here a <a href="https://youtu.be/QwC-IUb7t30" rel="nofollow noreferrer">Video</a> of the application speed</p>
  <p>PD. I'm so new on this</p>