diff --git a/dotnet/src/webdriver/Internal/PortUtilities.cs b/dotnet/src/webdriver/Internal/PortUtilities.cs
index a739e5ccd32f6..6f67d5abbd4b6 100644
--- a/dotnet/src/webdriver/Internal/PortUtilities.cs
+++ b/dotnet/src/webdriver/Internal/PortUtilities.cs
@@ -30,13 +30,23 @@ public static class PortUtilities
///
/// Finds a random, free port to be listened on.
///
+ ///
+ /// Prefers IPv4, but falls back to IPv6 if necessary.
+ ///
/// A random, free port to be listened on.
public static int FindFreePort()
{
- using var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- socket.DualMode = true;
- socket.Bind(new IPEndPoint(IPAddress.IPv6Loopback, 0));
- return (socket.LocalEndPoint as IPEndPoint)!.Port;
-
+ try
+ {
+ using var ipV4socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ ipV4socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
+ return ((IPEndPoint)ipV4socket.LocalEndPoint!).Port;
+ }
+ catch (SocketException)
+ {
+ using var ipV6socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
+ ipV6socket.Bind(new IPEndPoint(IPAddress.IPv6Loopback, 0));
+ return ((IPEndPoint)ipV6socket.LocalEndPoint!).Port;
+ }
}
}