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

Code before:
                            }

                            if (internalArgs.SocketError != SocketError.IOPending)
                            {
                                if (nextAddressIndex >= 0)
                                {
                                    address = addresses[nextAddressIndex];
                                    nextAddressIndex = GetNextAddressIndex(addresses, currentAddressFamily, nextAddressIndex);
                                }
                                else
                                {
                                    address = null;
                                    currentAddressFamily = AddressFamily.InterNetworkV6;
                                }
                                internalArgs.SocketError = SocketError.IOPending;
                            }

                            if (internalArgs.SecondarySaea is not null && (internalArgs.SecondarySaea.SocketError != SocketError.IOPending || nextIPv6AddressIndex > 0))
                            {
                                if (nextIPv6AddressIndex >= 0)
                                {
                                    address2 = addresses[nextIPv6AddressIndex];
                                    if (address is null)
                                    {
                                        // fall-back to normal processing without extra SAE
                                        address = addresses[nextIPv6AddressIndex];
                                        internalArgs.SocketError = SocketError.IOPending;
                                        address2 = null;
                                        internalArgs.SecondarySaea.Dispose();
                                        internalArgs.SecondarySaea = null;
                                    }
                                    else
                                    {
                                        Debug.Assert(internalArgs.SecondarySaea != null);
                                        address2 = addresses[nextIPv6AddressIndex];
                                        internalArgs.SecondarySaea!.SocketError = SocketError.IOPending;
                                    }

                                    nextIPv6AddressIndex = GetNextAddressIndex(addresses, AddressFamily.InterNetworkV6, nextIPv6AddressIndex);
                                }
                                else
                                {
                                    address2 = null;
                                }
                            }
                        }

                        Socket? attemptSocket = null;
                        Socket? attemptSocket2 = null;
                        if (_currentSocket != null)
                        {

Review comment (suggestion):
```suggestion
                                        address = address2;
```

Code after:
                                attemptSocket = tempSocketIPv4 ??= (Socket.OSSupportsIPv4 ? new Socket(AddressFamily.InterNetwork, socketType, protocolType) : null);
                            }

                            // If we were unable to get a socket to use for this address, move on to the next address.
                            if (attemptSocket is null)
                            {
                                continue;
                            }
                        }

                        // Reset the socket if necessary to support another connect.  This is necessary on Unix in particular where
                        // the same socket handle can't be used for another connect, so we swap in a new handle under the covers if
                        // possible.  We do this not just for the 2nd+ address but also for the first in case the Socket was already
                        // used for a connection attempt outside of this call.
                        attemptSocket.ReplaceHandleIfNecessaryAfterFailedConnect();

                        // Reconfigure the internal event args for the new address.
                        if (internalArgs.RemoteEndPoint is IPEndPoint existing)
                        {
                            existing.Address = address;
                            Debug.Assert(existing.Port == port);
                        }
                        else
                        {
                            internalArgs.RemoteEndPoint = new IPEndPoint(address, port);
                        }

                        // Issue the connect.  If it pends, wait for it to complete.
                        if (attemptSocket.ConnectAsync(internalArgs, userSocket: true, saeaMultiConnectCancelable: false, cancellationToken))
                        {
                            await new ValueTask(internalArgs, internalArgs.Version).ConfigureAwait(false);
                        }

                        // If it completed successfully, we're done; cleanup will be handled by the finally.
                        if (internalArgs.SocketError == SocketError.Success)
                        {
                            return;
                        }

                        // If the operation was canceled, simulate the appropriate SocketError.
                        if (cancellationToken.IsCancellationRequested)
                        {
                            lastError = SocketError.OperationAborted;
                            break;
                        }

                        lastError = internalArgs.SocketError;

                        // If multi-connect is no longer possible, terminate propagating the last error.
                        if (!attemptSocket.CanProceedWithMultiConnect)
                        {