title: Socket.DuplicateAndClose with async/await fails with E_INVALIDARG
body: <p>I'm having trouble using Socket.DuplicateAndClose in tandem with C#'s async/await feature. I've isolated the problem to a minimal test case, shown below. Note that using <code>writer.WriteLine</code> instead of <code>WriteLineAsync</code> (and <code>Flush</code> instead of <code>FlushAsync</code>) makes the program succeed without errors. Introducing async/await, however, throws an exception on the <code>WriteLineAsync("Second socket")</code> line.</p>
  
  <p>Apologies for the rather long minimal test case, it's as small as I could get it.</p>
  
  <p>Is it a bug in .net? Or am I doing something wrong?</p>
  
  <pre><code>using System;
  using System.Diagnostics;
  using System.IO;
  using System.Net;
  using System.Net.Sockets;
  using System.Threading;
  using System.Threading.Tasks;
  
  namespace SocketTest
  {
      class Program
      {
          // Simple hosting socket, not actually part of the problem, just something to connect to in Main()
          static void HostSocket(object dummy)
          {
              var listen = new TcpListener(IPAddress.Any, 12345);
              listen.Start();
              var client = listen.AcceptTcpClient();
              listen.Stop();
              var reader = new StreamReader(client.GetStream());
              while (true)
              {
                  string line;
                  try
                  {
                      line = reader.ReadLine();
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine("Server exception:");
                      Console.WriteLine(e);
                      break;
                  }
                  if (line == null)
                      break;
                  Console.WriteLine("Server: " + line);
              }
              reader.Dispose();
              Console.WriteLine("Server closed");
          }
  
          // The main problem
          static async Task RealMain()
          {
              // Connect to localhost and write a line to it. This works.
              var client = new TcpClient("127.0.0.1", 12345);
              var writer = new StreamWriter(client.GetStream());
              await writer.WriteLineAsync("First socket");
              await writer.FlushAsync();
              // Duplicate the socket with Socket.DuplicateAndClose, passing the current PID
              var info = client.Client.DuplicateAndClose(Process.GetCurrentProcess().Id);
              // Close() must be after Duplicate, otherwise ObjectDisposedException
              writer.Close();
              // Create a TcpClient using the SocketInformation struct obtained from DuplicateAndClose
              var second = new TcpClient();
              second.Client = new Socket(info);
              writer = new StreamWriter(second.GetStream());
              // Write the second line from the new socket
              await writer.WriteLineAsync("Second socket"); // EXCEPTION HERE
              await writer.FlushAsync();
              writer.Close();
          }
  
          // Boot the server and then start the client
          static void Main(string[] args)
          {
              new Thread(HostSocket) { IsBackground = true }.Start();
              Thread.Sleep(100);
              RealMain().Wait();
              Console.ReadKey(true);
          }
      }
  }
  </code></pre>
  
  <p>The exception is:</p>
  
  <pre><code>System.AggregateException was unhandled
    HResult=-2146233088
    Message=One or more errors occurred.
    Source=mscorlib
    StackTrace:
         at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
         at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
         at System.Threading.Tasks.Task.Wait()
         at SocketTest.Program.Main(String[] args) in c:\users\khyperia\documents\visual studio 2015\Projects\SocketTest\Program.cs:line 69
         at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
         at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
         at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
    InnerException: 
         HResult=-2146232800
         Message=Unable to write data to the transport connection: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)).
         Source=System
         StackTrace:
              at System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
              at System.IO.Stream.&lt;BeginEndWriteAsync&gt;b__16(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state)
              at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod)
              at System.IO.Stream.BeginEndWriteAsync(Byte[] buffer, Int32 offset, Int32 count)
              at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
              at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count)
              at System.IO.StreamWriter.&lt;FlushAsyncInternal&gt;d__1e.MoveNext()
           --- End of stack trace from previous location where exception was thrown ---
              at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
              at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
              at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
              at SocketTest.Program.&lt;RealMain&gt;d__1.MoveNext() in c:\users\khyperia\documents\visual studio 2015\Projects\SocketTest\Program.cs:line 60
         InnerException: 
              HResult=-2147024809
              Message=The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
              Source=mscorlib
              StackTrace:
                   at System.Threading.ThreadPool.BindIOCompletionCallbackNative(IntPtr fileHandle)
                   at System.Threading.ThreadPool.BindHandle(SafeHandle osHandle)
                   at System.Net.Sockets.Socket.BindToCompletionPort()
                   at System.Net.Sockets.BaseOverlappedAsyncResult.SetUnmanagedStructures(Object objectsToPin)
                   at System.Net.Sockets.OverlappedAsyncResult.SetUnmanagedStructures(Byte[] buffer, Int32 offset, Int32 size, SocketAddress socketAddress, Boolean pinSocketAddress)
                   at System.Net.Sockets.Socket.DoBeginSend(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, OverlappedAsyncResult asyncResult)
                   at System.Net.Sockets.Socket.BeginSend(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, SocketError&amp; errorCode, AsyncCallback callback, Object state)
                   at System.Net.Sockets.Socket.BeginSend(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state)
                   at System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
              InnerException: 
  </code></pre>