throw new ArgumentOutOfRangeException(nameof(port));
}
+ _family = AddressFamily.Unknown;
+
try
{
Connect(hostname, port);
if ((address.AddressFamily == AddressFamily.InterNetwork && Socket.OSSupportsIPv4) || Socket.OSSupportsIPv6)
{
var socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
+ if (address.IsIPv4MappedToIPv6)
+ {
+ socket.DualMode = true;
+ }
+
// Use of Interlocked.Exchanged ensures _clientSocket is written before Disposed is read.
Interlocked.Exchange(ref _clientSocket!, socket);
if (Disposed)
}
}
+ [Theory]
+ [InlineData(false, "::ffff:127.0.0.1")]
+ [InlineData(false, "127.0.0.1")]
+ [InlineData(false, "localhost")]
+ [InlineData(true, "::1")]
+ public void CtorConnect_Success(bool useIPv6, string connectString)
+ {
+ if (!Socket.OSSupportsIPv6)
+ {
+ return;
+ }
+
+ IPAddress serverAddress = useIPv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback;
+
+ using (var server = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
+ {
+ // Set up a server socket to which to connect
+ server.Bind(new IPEndPoint(serverAddress, 0));
+ server.Listen(1);
+ var endpoint = (IPEndPoint)server.LocalEndPoint;
+
+ using (TcpClient client = new TcpClient(connectString, endpoint.Port))
+ {
+ Assert.True(client.Connected);
+ }
+ }
+ }
+
private sealed class DerivedTcpClient : TcpClient
{
public new bool Active