Repository: dotnet/runtime
File: src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketAsyncEventArgsTest.cs (C#)

Code before:
            saea.RemoteEndPoint = receiver1.LocalEndPoint;
            saea.Completed += delegate { mres.Set(); };
            if (sender.SendToAsync(saea))
            {
                // did not finish synchronously.
                mres.Wait();
            }

            SocketReceiveFromResult result = await receiver1.ReceiveFromAsync(receiveBuffer, remoteEp).WaitAsync(TestSettings.PassingTestTimeout);
            Assert.Equal(sendBuffer.Length, result.ReceivedBytes);
            mres.Reset();


            saea.RemoteEndPoint = receiver2.LocalEndPoint;
            if (sender.SendToAsync(saea))
            {
                // did not finish synchronously.
                mres.Wait();
            }

            result = await receiver2.ReceiveFromAsync(receiveBuffer, remoteEp).WaitAsync(TestSettings.PassingTestTimeout);
            Assert.Equal(sendBuffer.Length, result.ReceivedBytes);
        }

        [ConditionalFact(typeof(DualModeBase), nameof(DualModeBase.LocalhostIsBothIPv4AndIPv6))]
        public void Connect_Parallel_Succss()
        {
            using PortBlocker portBlocker = new PortBlocker(() =>
            {
                Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                socket.DualMode = false;
                socket.BindToAnonymousPort(IPAddress.IPv6Loopback);
                return socket;
            });
            Socket a = portBlocker.MainSocket;
            Socket b = portBlocker.SecondarySocket;

            a.Listen(1);
            b.Listen(1);
            Task<Socket> t1 = a.AcceptAsync();
            Task<Socket> t2 = b.AcceptAsync();

            var mres = new ManualResetEventSlim();
            SocketAsyncEventArgs saea = new SocketAsyncEventArgs();
            saea.RemoteEndPoint = new DnsEndPoint("localhost", portBlocker.Port);
            saea.Completed += (_, _) => mres.Set();
            if (Socket.ConnectAsync(a.SocketType, a.ProtocolType, saea, ConnectAlgorithm.Parallel))
            {
                mres.Wait(TestSettings.PassingTestTimeout);
            }
            // we should see attemopt to both sockets

Review comment (suggestion):
```suggestion
        public void Connect_Parallel_Success()
```

Code after:

            saea.SetBuffer(sendBuffer);
            saea.RemoteEndPoint = receiver1.LocalEndPoint;
            saea.Completed += delegate { mres.Set(); };
            if (sender.SendToAsync(saea))
            {
                // did not finish synchronously.
                mres.Wait();
            }

            SocketReceiveFromResult result = await receiver1.ReceiveFromAsync(receiveBuffer, remoteEp).WaitAsync(TestSettings.PassingTestTimeout);
            Assert.Equal(sendBuffer.Length, result.ReceivedBytes);
            mres.Reset();


            saea.RemoteEndPoint = receiver2.LocalEndPoint;
            if (sender.SendToAsync(saea))
            {
                // did not finish synchronously.
                mres.Wait();
            }

            result = await receiver2.ReceiveFromAsync(receiveBuffer, remoteEp).WaitAsync(TestSettings.PassingTestTimeout);
            Assert.Equal(sendBuffer.Length, result.ReceivedBytes);
        }

        [ConditionalFact(typeof(DualModeBase), nameof(DualModeBase.LocalhostIsBothIPv4AndIPv6))]
        public void Connect_Parallel_Success()
        {
            using PortBlocker portBlocker = new PortBlocker(() =>
            {
                Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                socket.DualMode = false;
                socket.BindToAnonymousPort(IPAddress.IPv6Loopback);
                return socket;
            });
            Socket a = portBlocker.MainSocket;
            // the port blocker did not call Socket.Bind so we called bind() but we did not update properties on Socket
            Socket b  = new Socket(portBlocker.SecondarySocket.SafeHandle);

            a.Listen(1);
            b.Listen(1);
            Task<Socket> t1 = a.AcceptAsync();
            Task<Socket> t2 = b.AcceptAsync();

            var mres = new ManualResetEventSlim();
            SocketAsyncEventArgs saea = new SocketAsyncEventArgs();
            saea.RemoteEndPoint = new DnsEndPoint("localhost", portBlocker.Port);
            saea.Completed += (_, _) => mres.Set();
            if (Socket.ConnectAsync(a.SocketType, a.ProtocolType, saea, ConnectAlgorithm.Parallel))
            {