title: UDPClient Async BeginReceive is very slow
body: <p>I am using UDPClient to send multicast request and receive response from various clients on the network.  I am able to send request and also getting response, but response that I get is very slow. It take 2-3 minutes to get response from all the clients. On send request on the network I checked in the WireShark, there I am seeing response in milliseconds from all the clients only in test program. It is taking lot of time. Can anyone guide what mistake I might be doing? Following is the code. Please guide me on this.  I have been stuck on this issue for the  last 2 days.</p>
  
  <pre><code>public class Trinity_WSDiscovery : IDiscoveryService 
  {
          #region IDiscoveryService Members
          public event EventHandler FoundNewDevice;
          public event EventHandler EndOfDiscovery;
          DeviceBinding m_DeviceBinding;
          bool IsFindComplete = false;
          Thread receiveThread;
          UdpClient sock ;        
          IPEndPoint RemoteIpEndPoint = new IPEndPoint(System.Net.IPAddress.Any, 0);
          IPEndPoint iep = new IPEndPoint(System.Net.IPAddress.Parse("239.255.255.250"), 3702);
          UdpState udpState = new UdpState();
          XmlDocument xmlDoc = new XmlDocument();
  
      public void Start()
      {
          //Need to create new object every time we start discovery because
          //every time udp buffer needs to be flushed and restarted
          sock = new UdpClient();
          string str = "&lt;?xml version='1.0' encoding='utf-8'?&gt;&lt;soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:d=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" xmlns:wsadis=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:dn=\"http://www.onvif.org/ver10/network/wsdl\"&gt;&lt;soap:Header&gt;&lt;wsadis:MessageID&gt;uuid:" + System.Guid.NewGuid().ToString() + "&lt;/wsadis:MessageID&gt;&lt;wsadis:To&gt;urn:schemas-xmlsoap-org:ws:2005:04:discovery&lt;/wsadis:To&gt;&lt;wsadis:Action&gt;http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe&lt;/wsadis:Action&gt;&lt;/soap:Header&gt;&lt;soap:Body&gt;&lt;d:Probe&gt;&lt;d:Types /&gt; &lt;d:Scopes/&gt;&lt;/d:Probe&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;";
          byte[] data = Encoding.ASCII.GetBytes(str);
          sock.Send(data, data.Length, iep);
          sock.JoinMulticastGroup(System.Net.IPAddress.Parse("239.255.255.250"));            
          IPEndPoint iep1 = new IPEndPoint(System.Net.IPAddress.Any, 0);            
          udpState.ipEndpt = RemoteIpEndPoint;
          udpState.udpClient = sock;           
          BeginReceive();           
      }
  
      public void BeginReceive()
      {
          Thread.Sleep(100);
          if (sock.Available &gt; 0)
          {
              sock.BeginReceive(new AsyncCallback(ReceiveCallback), udpState);
          }
          else
          {
              FindComplete();
          }
      }
  
      public void ReceiveCallback(IAsyncResult ar)
      {
          UdpClient udpClient = (UdpClient)((UdpState)(ar.AsyncState)).udpClient;
          IPEndPoint ipEndpt = (IPEndPoint)((UdpState)(ar.AsyncState)).ipEndpt;
          Byte[] receiveBytes = udpClient.EndReceive(ar, ref ipEndpt);
          string receiveString = Encoding.ASCII.GetString(receiveBytes);
          if (receiveString.Contains("NetworkVideoTransmitter"))
          {
              xmlDoc.LoadXml(receiveString);
              XmlNodeList list = xmlDoc.GetElementsByTagName("XAddrs", "http://schemas.xmlsoap.org/ws/2005/04/discovery");
              XmlNode node = list[0];
              string strEndPoints = node.FirstChild.Value;
              string[] strEndPointList = Regex.Split(strEndPoints, " ");
              OnFoundDevice(strEndPointList);
          }
          BeginReceive();
      }
  </code></pre>
  
  <p>}`</p>