title: How to properly listen for UDP packets while doing other things in Visual C#
body: <p>Alright, so I'm pretty new to C# and I'm definitely pretty new to graphical programming. I'm using Visual Studio 2015 and writing my application in C#.
  I have this hunk of code that I've been toying around with for a while. Essentially my program will send the HELLO, to the server, but the server isn't sending HELLO back. I have no firewall in the middle of client and server right now, but the process is getting hung waiting for the reply back. I honestly don't even want to do it this way, I want the listener to always run in the background while the user does other stuff so that my program functions, well normal. So I come to you oh great Stackoverflow... because I am definitely doing it wrong! Could someone please point me the right direction?</p>
  
  <p><em>Current Code:</em></p>
  
  <pre><code>    private void button1_Click(object sender, EventArgs e)
      {
          byte[] data = new byte[512];
          byte[] result;
          SHA512 shaM = new SHA512Managed();
          result = shaM.ComputeHash(Encoding.UTF8.GetBytes(this.password.Text));
          var hash = BitConverter.ToString(result).Replace("-", "");
          this.send_message("127.0.0.1", 10545, 10545, "HELLO");
          this.send_message("127.0.0.1", 10545, 10545, "AUTH:" + this.login.Text + ":" + hash);
  
          //ListenForData.Start();
  
      }
  
      private void send_message(string server, int localPort, int remotePort, string message)
      {
          label4.Text = "Listening on port:" + localPort;
          IPEndPoint lep = new IPEndPoint(IPAddress.Any, localPort);
          Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
          IPAddress loginServer = IPAddress.Parse(server.ToString());
          byte[] sendbuf = Encoding.ASCII.GetBytes(message);
          IPEndPoint ep = new IPEndPoint(loginServer, remotePort);
          s.SendTo(sendbuf, ep);
  
          try
          {
              UdpClient udpClient = new UdpClient(lep);
              byte[] bytes = udpClient.Receive(ref lep);
              label4.Text = ep.ToString();
          } catch ( Exception ex )
          {
              Console.WriteLine(ex.ToString());
          }
  
      }
  </code></pre>
  
  <p><strong>UDPATE:</strong></p>
  
  <pre><code>   private static void UDPListener(object obj)
      {
          Task.Run(async () =&gt;
          {
              using (var udpClient = new UdpClient(10545))
              {
                  string rMessage = "";
                  string[] rArgs = new string[0];
                  while (true)
                  {
                      //IPEndPoint object will allow us to read datagrams sent from any source.
                      var receivedResults = await udpClient.ReceiveAsync();
                      rMessage += Encoding.ASCII.GetString(receivedResults.Buffer);
                      rArgs = rMessage.Split(new char[] { ':' });
                      if( rArgs[0] == "HELLO")
                      {
                          Console.Write("Received HELLO from server.");
                          byte[] data = new byte[512];
                          byte[] result;
                          SHA512 shaM = new SHA512Managed();
                          result = shaM.ComputeHash(Encoding.UTF8.GetBytes(obj.password.Text));
                          var hash = BitConverter.ToString(result).Replace("-", "");
                          send_message("127.0.0.1", 10545, 10545, "AUTH:" + obj.login.Text + ":" + hash);
                      }
                  }
              }
          });
      }
  </code></pre>