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

Code before:
            {
                _saea = saea;
            }
            public bool ReachedCoordinationPointFirst() => !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 firstFinal = false;

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

                if (firstFinal)
                {
                    // 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);
                }
            }
        }


        internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags)
        {
            SetResults(SocketError.Success, bytesTransferred, flags);

            if (NetEventSource.Log.IsEnabled() && bytesTransferred > 0)
            {
                LogBuffer(bytesTransferred);
            }

Review comment (suggestion):
Nit: this comment appears 2 times, I would place it over the `firstFinal = ReachedCoordinationPointFirst();` call instead.

Code after:

            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)
                {
                    // 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);
                }
            }
        }


        internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags)
        {
            SetResults(SocketError.Success, bytesTransferred, flags);