title: SSDP discovery in Windows 8 Metro not reliable
body: <p>I am having problem implementing SSDP discovery in windows 8 metro program. Below is the code:</p>
  
  <pre><code>public async Task SearchForDevices()
      {
          var socket = new DatagramSocket();
          socket.MessageReceived += async (sender, args) =&gt;
                                              {
                                                  Debug.WriteLine("Received data" + DateTime.Now);
                                                  DataReader reader = args.GetDataReader();
  
                                                  uint count = reader.UnconsumedBufferLength;
                                                  string data = reader.ReadString(count);
                                                  Debug.WriteLine(data);
                                                  var response = new Dictionary&lt;string, string&gt;();
                                                  foreach (
                                                      string x in
                                                          data.Split(new[] {"\r\n", "\n"}, StringSplitOptions.None))
                                                  {
                                                      if (x.Contains(":"))
                                                      {
                                                          string[] strings = x.Split(':');
                                                          response.Add(strings[0].ToLower(), x.Remove(0, strings[0].Length + 1));
                                                      }
                                                  }
  
                                                  Device device = await GetXml(response);
                                                  Debug.WriteLine("Device found");
                                                  if (DeviceFound != null)
                                                      DeviceFound(this, new DeviceFoundEventArgs(device));
                                              };
          await socket.BindEndpointAsync(null, "");
          HostName hostName = new HostName("239.255.255.250");
          socket.JoinMulticastGroup(hostName);
  
          string message = "M-SEARCH * HTTP/1.1\r\n" +
                                 "HOST: " + hostName.DisplayName + ":1900\r\n" +
                                 "ST: upnp:rootdevice\r\n" +
                                 "MAN: \"ssdp:discover\"\r\n" +
                                 "MX: 3\r\n\r\n";
  
          DateTime start = DateTime.Now;
          TimeSpan interval = new TimeSpan(0, 0, 30);
          Debug.WriteLine(start);
          IOutputStream stream = await socket.GetOutputStreamAsync(hostName, "1900");
          var writer = new DataWriter(stream) { UnicodeEncoding = UnicodeEncoding.Utf8 };
          writer.WriteString(message);
          await writer.StoreAsync();
          await Task.Delay(1500);
      }
  </code></pre>
  
  <p>This code is not reliable at all. about 50% times it can't find a connected device while other discover can find. But sometimes it works.</p>
  
  <p>My network sniffer (SmartSniff) sometimes can't even capture its SSDP request even though I have made the call many times.</p>
  
  <p>I don't think this is an async problem since it does work occasionally. 
  Please help me !
  Thanks!</p>