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

Code before:
            protected override void OnCompleted(SocketAsyncEventArgs e) =>_mrvtsc.SetResult(true);

            public bool ReachedCoordinationPointFirst() => !Interlocked.Exchange(ref _isCompleted, true);
        }

        private sealed class ParallelMultiConnectSocketState
        {
            private bool _isCompleted;
            private int _count;
            private SocketAsyncEventArgs _saea;

            public ParallelMultiConnectSocketState(SocketAsyncEventArgs saea)
            {
                _saea = saea;
            }
            public bool Finished() => Interlocked.Exchange(ref _isCompleted, true);

            public void SetResults(Socket? socket, SocketError socketError, int bytesTransferred, SocketFlags flags, Exception? exception)
            {
                int count = Interlocked.Increment(ref _count);
                bool shouldComplete = false;

                if (socketError == SocketError.Success && exception == null)
                {
                    shouldComplete = !Finished();
                    if (shouldComplete)
                    {
                        _saea._connectSocket = _saea._currentSocket = socket;
                        _saea.SetResults(SocketError.Success, bytesTransferred, flags);
                    }
                }
                else if (count == 2)    // We ignore failures on first socket since we have one more pending.
                {
                    shouldComplete = !Finished();
                    if (shouldComplete)
                    {
                        _saea.SetResults(exception!, 0, SocketFlags.None);
                        _saea._currentSocket?.UpdateStatusAfterSocketError(_saea._socketError);
                    }
                }

                if (shouldComplete)
                {
                    // If this is the first final result, we need to complete the operation and release underlying SocketAsyncEventArgs
                    _saea.Complete();
                    if (SocketsTelemetry.Log.IsEnabled()) LogBytesTransferEvents(socket?.SocketType, SocketAsyncOperation.Connect, bytesTransferred);
                    // signal caller we are done.
                    _saea.OnCompleted(_saea);
                }
            }
        }

Review comment (suggestion):
It seems like, if both socket is successful, and when second one hits here, it's not getting disposed, and because we already marked `_isCompleted` as true with first socket, basically this method will do nothing for the second successful socket, if I'm not missing anything.

Code after:
            public short Version => _mrvtsc.Version;
            public void Reset() => _mrvtsc.Reset();

            protected override void OnCompleted(SocketAsyncEventArgs e) =>_mrvtsc.SetResult(true);

            public bool ReachedCoordinationPointFirst() => !Interlocked.Exchange(ref _isCompleted, true);
        }

        private sealed class ParallelMultiConnectSocketState
        {
            private bool _isCompleted;
            private int _count;
            private SocketAsyncEventArgs _saea;

            public ParallelMultiConnectSocketState(SocketAsyncEventArgs saea)
            {
                _saea = saea;
            }
            public bool Finished() => Interlocked.Exchange(ref _isCompleted, true);

            public void SetResults(Socket? socket, SocketError socketError, int bytesTransferred, SocketFlags flags, Exception? exception)
            {
                int count = Interlocked.Increment(ref _count);
                bool shouldComplete = false;

                if (socketError == SocketError.Success && exception == null)
                {
                    shouldComplete = !Finished();
                    if (shouldComplete)
                    {
                        _saea._connectSocket = _saea._currentSocket = socket;
                        _saea.SetResults(SocketError.Success, bytesTransferred, flags);
                    }
                    else
                    {
                        // Another parallel connect already won - dispose the losing socket.
                        socket?.Dispose();
                    }
                }
                else if (count == 2)    // We ignore failures on first socket since we have one more pending.
                {
                    shouldComplete = !Finished();
                    if (shouldComplete)
                    {
                        _saea.SetResults(exception!, 0, SocketFlags.None);
                        _saea._currentSocket?.UpdateStatusAfterSocketError(_saea._socketError);
                    }
                }

                if (shouldComplete)
                {