title: C# Windows Phone 8.1 GetOutputStreamAsync(...) blocking after first use
body: <p>i'm currently trying to create a very basic test app which should:</p>
  
  <p>1) Broadcast "sometext" on port "1234"<br>
  2) Wait a second for answers<br>
  3) Return all answers  </p>
  
  <p>While the solution posted below works fine for the first time, every subsequent call blocks forever at:<br>
  <i>stream = await socket.GetOutputStreamAsync(...) </i></p>
  
  <p>Till now i tried every possible way of cleaning up (since thats where i suppose the failure), even wrapping everything in <i>using(...)</i> statements.</p>
  
  <p>The problem occurs with the emulator as well as a hardware device using Windows Phone 8.1</p>
  
  <hr>
  
  <p>Thanks in advance!</p>
  
  <hr>
  
  <p>The code to start the "discovery":</p>
  
  <pre class="lang-cs prettyprint-override"><code>private void Button_Click(object sender, RoutedEventArgs e)
  {
      PluginUDP pudp = new PluginUDP();
      var task = pudp.scan("asf");
      task.Wait();
      foreach (string s in task.Result)
          output.Text += s + "\r\n";            
  }
  </code></pre>
  
  <hr>
  
  <p>The code for the "discovery" itself:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using Windows.Networking;
  using Windows.Networking.Sockets;
  using Windows.Storage.Streams;
  using System.Text;
  using System.IO;
  using System.Threading.Tasks;
  using System.Collections.Concurrent;
  
  using namespace whatever
  {
      public class PluginUDP
      {
          private static readonly HostName BroadcastAddress = new HostName("255.255.255.255");
          private static readonly string BroadcastPort = "1234";
          private static readonly byte[] data = Encoding.UTF8.GetBytes("00wlan-ping00");
  
          ConcurrentBag&lt;string&gt; receivers;        
  
          public async System.Threading.Tasks.Task&lt;string[]&gt; scan(string options)
          {
              receivers = new ConcurrentBag&lt;string&gt;();
              receivers.Add("ok");
  
              DatagramSocket socket = null;
              IOutputStream stream = null;
              DataWriter writer = null;
  
              try
              {
                  socket = new DatagramSocket();                
                  socket.MessageReceived += MessageReceived;
  
                  await socket.BindServiceNameAsync("");
  
                  stream = await socket.GetOutputStreamAsync(BroadcastAddress, BroadcastPort);
                  writer = new DataWriter(stream);               
  
                  writer.WriteBytes(data);
                  await writer.StoreAsync();
  
                  Task.Delay(1000).Wait();                
              }
              catch (Exception exception)
              {
                  receivers.Add(exception.Message);
              }
              finally
              {
                  if (writer != null)
                  {
                      writer.DetachStream();
                      writer.Dispose();
                  }
  
                  if(stream != null)
                      stream.Dispose();
  
                  if(socket != null)
                      socket.Dispose();
              }
  
              return receivers.ToArray(); ;
          }
  
          private async void MessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs args)
          {
              try
              {
                  var result = args.GetDataStream();
                  var resultStream = result.AsStreamForRead(1024);
  
                  using (var reader = new StreamReader(resultStream))
                  {
                      var text = await reader.ReadToEndAsync();
                      if (text.Contains("pong"))
                      {
                          receivers.Add(args.RemoteAddress.ToString());
                      }
                  }
              }
              catch (Exception exception)
              {
                  receivers.Add("ERRCV");
              }
          }
      }
  }
  </code></pre>