Skip to content

Commit 6e36192

Browse files
committed
Use DualMode
1 parent 264d1af commit 6e36192

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

dotnet/src/webdriver/Internal/PortUtilities.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,10 @@ public static class PortUtilities
3333
/// <returns>A random, free port to be listened on.</returns>
3434
public static int FindFreePort()
3535
{
36-
// Locate a free port on the local machine by binding a socket to
37-
// an IPEndPoint using IPAddress.Any and port 0. The socket will
38-
// select a free port.
39-
try
40-
{
41-
using var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
42-
socket.Bind(new IPEndPoint(IPAddress.IPv6Loopback, 0));
43-
return (socket.LocalEndPoint as IPEndPoint)!.Port;
44-
}
45-
catch (SocketException)
46-
{
47-
// If IPv6 is not supported, fallback to IPv4
48-
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
49-
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
50-
return (socket.LocalEndPoint as IPEndPoint)!.Port;
51-
}
36+
using var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
37+
socket.DualMode = true;
38+
socket.Bind(new IPEndPoint(IPAddress.IPv6Loopback, 0));
39+
return (socket.LocalEndPoint as IPEndPoint)!.Port;
40+
5241
}
5342
}

0 commit comments

Comments
 (0)