public static unsafe SocketError ConnectAsync(Socket socket, SafeCloseSocket handle, byte[] socketAddress, int socketAddressLen, ConnectOverlappedAsyncResult asyncResult) { // This will pin the socketAddress buffer. asyncResult.SetUnmanagedStructures(socketAddress); try { int ignoreBytesSent; bool success = socket.ConnectEx( handle, Marshal.UnsafeAddrOfPinnedArrayElement(socketAddress, 0), socketAddressLen, IntPtr.Zero, 0, out ignoreBytesSent, asyncResult.OverlappedHandle); return(asyncResult.ProcessOverlappedResult(success, 0)); } catch { asyncResult.ReleaseUnmanagedStructures(); throw; } }