title: UDP Listener Server Fails To Capture Incoming Data
body: <ul>
  <li><p>I have a device with IP X.X.X.X which is not on the same
  network as my UDP Server.</p></li>
  <li><p>This device with IP X.X.X.X is sending packets with UDP protocol to
  my UDP Server. </p></li>
  <li><p>Packets are sent to port 5000 of my UDP Server</p></li>
  <li><p>WireShark is showing that my PC is receiving packets from the device.</p>
  
  <pre><code>2711    351.573764  X.X.X.X  10.0.0.4   UDP 83    57514 → 5000   Len=61
  </code></pre></li>
  <li><p>I want to capture the sent packets for processing. </p></li>
  <li><p>The following code is being taken from this <a href="https://acrocontext.wordpress.com/2013/08/15/c-simple-udp-listener-in-asynchronous-way/" rel="nofollow noreferrer">example</a> Currently, the moment program hits <em>Socket.BeginReceiveFrom</em>, Console application closes.</p></li>
  </ul>
  
  <p>I believe that I'm missing something extremely basic, but I'm not able to see it now, so anything, tips or suggestions would be welcomed.</p>
  
  <pre><code>    static private Socket Socket;
      static private byte[] Buffer = new byte[1024];
  
      static void Main(string[] args)
      {
          StartServer();
      }
  
      static public void StartServer()
      {
          //Setup the socket and message buffer
          Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
          Socket.Bind(new IPEndPoint(IPAddress.Any, 5000));
  
          //Start listening for a new message.
          EndPoint ClientEndPoint = new IPEndPoint(IPAddress.Any, 5000);
          Socket.BeginReceiveFrom(Buffer, 0, Buffer.Length, SocketFlags.None, ref ClientEndPoint, Receiver, Socket);
      }
  
      static private void Receiver(IAsyncResult iar)
      {
          //Get the received message.
          Socket ReceiveSocket = (Socket)iar.AsyncState;
          EndPoint ClientEndPoint = new IPEndPoint(IPAddress.Any, 5000);
          int MessageLength = ReceiveSocket.EndReceiveFrom(iar, ref ClientEndPoint);
          byte[] localMsg = new byte[MessageLength];
          Array.Copy(Buffer, localMsg, MessageLength);
  
          //Start listening for a new message.
          EndPoint NewClientEndPoint = new IPEndPoint(IPAddress.Any, 5000);
          Socket.BeginReceiveFrom(Buffer, 0, Buffer.Length, SocketFlags.None, ref NewClientEndPoint, Receiver, Socket);
  
          // Do something here
      }
  </code></pre>