title: StreamSocket closing unexpectedly
body: <p>I am trying to use a StreamSocket and a StreamSocketListener to create a proxy. The goal is to have the proxy do something, but right now I can't even get that far. The connection keeps getting closed when I try to write the response from the original destination to the client connected to the proxy. The stranger thing is it works sometimes, but not others, implying some sort of threading or issue perhaps? Currently this proxy is only serving one request at a time.</p>
  
  <pre><code>StreamSocketListener server = new StreamSocketListener();
  server.ConnectionReceived += this.server_ConnectionReceivedAsync;
  await server.BindServiceNameAsync("9000");
  </code></pre>
  
  <p>Here is the code that handles the connection</p>
  
  <pre><code>await originalDestination.ConnectAsync(new EndpointPair(null, string.Empty, this.hostName, this.port));
  // Read client data, and forward on to the original destination
  await ForwardStream(client.InputStream, originalDestination.OutputStream);
  // Get the response from the original destination, and forward it to the client
  await ForwardStream(originalDestination.InputStream, client.OutputStream);
  </code></pre>
  
  <p>and here is the ForwardStream function</p>
  
  <pre><code>bool readMore;
  do
  {
      Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer(1024);
      await reader.ReadAsync(buffer, 1024, InputStreamOptions.Partial);
      readMore = buffer.Length == 1024 ? true : false;
      if (buffer.Length != 0)
      {
          // This is where I get the socket closed exception
          await writer.WriteAsync(buffer);
      }
      else
      {
          return;
      }
  }
  while (readMore);
  </code></pre>