From 033aaa013a935eeae7970aafcf017cba0b8d58f8 Mon Sep 17 00:00:00 2001
From: Anton Firszov <antonfir@gmail.com>
Date: Thu, 1 Jul 2021 12:26:26 +0200
Subject: [PATCH 1/6] Remove reference to non-existent
 System.Net.NameResolution.Unit.Tests.csproj

---
 .../System.Net.NameResolution.sln                          | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/src/libraries/System.Net.NameResolution/System.Net.NameResolution.sln b/src/libraries/System.Net.NameResolution/System.Net.NameResolution.sln
index 83e8aa0d133cec..eb9d01b1c49b68 100644
--- a/src/libraries/System.Net.NameResolution/System.Net.NameResolution.sln
+++ b/src/libraries/System.Net.NameResolution/System.Net.NameResolution.sln
@@ -11,8 +11,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Net.NameResolution.F
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Net.NameResolution.Pal.Tests", "tests\PalTests\System.Net.NameResolution.Pal.Tests.csproj", "{72B2CC3A-D66E-4F7E-BFA4-38EE373B5043}"
 EndProject
-Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Net.NameResolution.Unit.Tests", "tests\UnitTests\System.Net.NameResolution.Unit.Tests.csproj", "{82007016-ED8D-408E-BA83-DFB3F74B104F}"
-EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Runtime.CompilerServices.Unsafe", "..\System.Runtime.CompilerServices.Unsafe\ref\System.Runtime.CompilerServices.Unsafe.csproj", "{A3FC70CE-3746-4E03-A92F-0102716FA97E}"
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Runtime.CompilerServices.Unsafe", "..\System.Runtime.CompilerServices.Unsafe\src\System.Runtime.CompilerServices.Unsafe.ilproj", "{D62E59F2-BDF3-4060-85E0-20D1BB41B95E}"
@@ -34,7 +32,6 @@ Global
 		{00BF602B-F709-4108-9A39-C57A493D3EF8} = {41E915D1-3857-4F01-8576-B24104359E0B}
 		{BE2A4103-7CA0-4DAD-A4AD-F623CBFAD379} = {41E915D1-3857-4F01-8576-B24104359E0B}
 		{72B2CC3A-D66E-4F7E-BFA4-38EE373B5043} = {41E915D1-3857-4F01-8576-B24104359E0B}
-		{82007016-ED8D-408E-BA83-DFB3F74B104F} = {41E915D1-3857-4F01-8576-B24104359E0B}
 		{77720BB9-1319-4BF7-A2DB-B1FC3F20FD1F} = {B9D5A47D-9CA5-4FE1-AFE2-5E85A940B43D}
 		{15B66DCE-70BF-43D6-854D-42A6C840B22C} = {B9D5A47D-9CA5-4FE1-AFE2-5E85A940B43D}
 		{A3FC70CE-3746-4E03-A92F-0102716FA97E} = {B9D5A47D-9CA5-4FE1-AFE2-5E85A940B43D}
@@ -73,10 +70,6 @@ Global
 		{72B2CC3A-D66E-4F7E-BFA4-38EE373B5043}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{72B2CC3A-D66E-4F7E-BFA4-38EE373B5043}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{72B2CC3A-D66E-4F7E-BFA4-38EE373B5043}.Release|Any CPU.Build.0 = Release|Any CPU
-		{82007016-ED8D-408E-BA83-DFB3F74B104F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{82007016-ED8D-408E-BA83-DFB3F74B104F}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{82007016-ED8D-408E-BA83-DFB3F74B104F}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{82007016-ED8D-408E-BA83-DFB3F74B104F}.Release|Any CPU.Build.0 = Release|Any CPU
 		{A3FC70CE-3746-4E03-A92F-0102716FA97E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{A3FC70CE-3746-4E03-A92F-0102716FA97E}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{A3FC70CE-3746-4E03-A92F-0102716FA97E}.Release|Any CPU.ActiveCfg = Release|Any CPU

From e6fc806631afdb3c900f1194f8ea200b0363d4b3 Mon Sep 17 00:00:00 2001
From: Anton Firszov <antonfir@gmail.com>
Date: Thu, 1 Jul 2021 15:28:26 +0200
Subject: [PATCH 2/6] switch to disable IPv6

---
 .../Net/SocketProtocolSupportPal.Unix.cs      |  6 +-
 .../Net/SocketProtocolSupportPal.Windows.cs   |  6 +-
 .../System/Net/SocketProtocolSupportPal.cs    | 42 ++++++++++++++
 .../src/System.Net.NameResolution.csproj      |  3 +
 .../tests/FunctionalTests/GetHostEntryTest.cs | 58 ++++++++++++++++---
 ...System.Net.NameResolution.Pal.Tests.csproj |  2 +
 .../src/System.Net.Ping.csproj                |  2 +
 .../src/System.Net.Sockets.csproj             |  2 +
 .../tests/FunctionalTests/OSSupport.cs        | 32 ++++++++++
 9 files changed, 135 insertions(+), 18 deletions(-)
 create mode 100644 src/libraries/Common/src/System/Net/SocketProtocolSupportPal.cs

diff --git a/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.Unix.cs b/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.Unix.cs
index d06ce8117fc5a2..8de1e2a0f4ce31 100644
--- a/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.Unix.cs
+++ b/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.Unix.cs
@@ -7,12 +7,8 @@
 
 namespace System.Net
 {
-    internal static class SocketProtocolSupportPal
+    internal static partial class SocketProtocolSupportPal
     {
-        public static bool OSSupportsIPv6 { get; } = IsSupported(AddressFamily.InterNetworkV6);
-        public static bool OSSupportsIPv4 { get; } = IsSupported(AddressFamily.InterNetwork);
-        public static bool OSSupportsUnixDomainSockets { get; } = IsSupported(AddressFamily.Unix);
-
         private static unsafe bool IsSupported(AddressFamily af)
         {
             IntPtr invalid = (IntPtr)(-1);
diff --git a/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.Windows.cs b/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.Windows.cs
index de0465a51c6c7e..50e7db176e2f14 100644
--- a/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.Windows.cs
+++ b/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.Windows.cs
@@ -9,12 +9,8 @@
 
 namespace System.Net
 {
-    internal static class SocketProtocolSupportPal
+    internal static partial class SocketProtocolSupportPal
     {
-        public static bool OSSupportsIPv6 { get; } = IsSupported(AddressFamily.InterNetworkV6);
-        public static bool OSSupportsIPv4 { get; } = IsSupported(AddressFamily.InterNetwork);
-        public static bool OSSupportsUnixDomainSockets { get; } = IsSupported(AddressFamily.Unix);
-
         private static bool IsSupported(AddressFamily af)
         {
             Interop.Winsock.EnsureInitialized();
diff --git a/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.cs b/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.cs
new file mode 100644
index 00000000000000..6e2642e00c271a
--- /dev/null
+++ b/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.cs
@@ -0,0 +1,42 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Net.Sockets;
+
+namespace System.Net
+{
+    internal static partial class SocketProtocolSupportPal
+    {
+        private const string DisableIPv6AppCtxSwitch = "System.Net.DisableIPv6";
+        private const string DisableIPv6EnvironmentVariable = "DOTNET_SYSTEM_NET_DISABLEIPV6";
+
+        public static bool OSSupportsIPv6 { get; } = IsSupported(AddressFamily.InterNetworkV6) && !IsIPv6Disabled();
+        public static bool OSSupportsIPv4 { get; } = IsSupported(AddressFamily.InterNetwork);
+        public static bool OSSupportsUnixDomainSockets { get; } = IsSupported(AddressFamily.Unix);
+
+        private static bool IsIPv6Disabled()
+        {
+            bool value;
+
+            // First check for the AppContext switch, giving it priority over the environment variable.
+            if (AppContext.TryGetSwitch(DisableIPv6AppCtxSwitch, out value))
+            {
+                return value;
+            }
+
+            // AppContext switch wasn't used. Check the environment variable.
+            string? envVar = Environment.GetEnvironmentVariable(DisableIPv6EnvironmentVariable);
+
+            if (bool.TryParse(envVar, out value))
+            {
+                return value;
+            }
+            else if (uint.TryParse(envVar, out uint intVal))
+            {
+                return intVal != 0;
+            }
+
+            return false;
+        }
+    }
+}
diff --git a/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj b/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj
index d2d9b9b46910cc..26f359db8d0404 100644
--- a/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj
+++ b/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj
@@ -1,6 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <IncludeRemoteExecutor>true</IncludeRemoteExecutor>
     <TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser</TargetFrameworks>
     <Nullable>enable</Nullable>
   </PropertyGroup>
@@ -31,6 +32,8 @@
              Link="Common\System\Net\IPAddressParserStatics.cs" />
     <Compile Include="$(CommonPath)System\Net\IPEndPointStatics.cs"
              Link="Common\System\Net\IPEndPointStatics.cs" />
+    <Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.cs"
+             Link="Common\System\Net\SocketProtocolSupportPal.cs" />
   </ItemGroup>
   <ItemGroup Condition=" '$(TargetsWindows)' == 'true'">
     <Compile Include="System\Net\NameResolutionPal.Windows.cs" />
diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs
index 6fa40dd80c46a0..4b5a735f670750 100644
--- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs
+++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs
@@ -6,7 +6,7 @@
 using System.Net.Sockets;
 using System.Threading;
 using System.Threading.Tasks;
-
+using Microsoft.DotNet.RemoteExecutor;
 using Xunit;
 
 namespace System.Net.NameResolution.Tests
@@ -20,12 +20,18 @@ public async Task Dns_GetHostEntryAsync_IPAddress_Ok()
             await TestGetHostEntryAsync(() => Dns.GetHostEntryAsync(localIPAddress));
         }
 
-        [ActiveIssue("https://github.com/dotnet/runtime/issues/1488", TestPlatforms.OSX)]
-        
-        [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotArm64Process))] // [ActiveIssue("https://github.com/dotnet/runtime/issues/27622")]
+        public static bool GetHostEntryWorks =
+            // [ActiveIssue("https://github.com/dotnet/runtime/issues/27622")]
+            PlatformDetection.IsNotArmNorArm64Process &&
+            // [ActiveIssue("https://github.com/dotnet/runtime/issues/1488", TestPlatforms.OSX)]
+            PlatformDetection.IsNotOSX &&
+            // [ActiveIssue("https://github.com/dotnet/runtime/issues/51377", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
+            !PlatformDetection.IsiOS && !PlatformDetection.IstvOS && !PlatformDetection.IsMacCatalyst;
+
+
+        [ConditionalTheory(nameof(GetHostEntryWorks))]
         [InlineData("")]
         [InlineData(TestSettings.LocalHost)]
-        [ActiveIssue("https://github.com/dotnet/runtime/issues/51377", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
         public async Task Dns_GetHostEntry_HostString_Ok(string hostName)
         {
             try
@@ -71,11 +77,9 @@ public async Task Dns_GetHostEntry_HostString_Ok(string hostName)
             }
         }
 
-        [ActiveIssue("https://github.com/dotnet/runtime/issues/1488", TestPlatforms.OSX)]
-        [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotArm64Process))] // [ActiveIssue("https://github.com/dotnet/runtime/issues/27622")]
+        [ConditionalTheory(nameof(GetHostEntryWorks))]
         [InlineData("")]
         [InlineData(TestSettings.LocalHost)]
-        [ActiveIssue("https://github.com/dotnet/runtime/issues/51377", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
         public async Task Dns_GetHostEntryAsync_HostString_Ok(string hostName) =>
             await TestGetHostEntryAsync(() => Dns.GetHostEntryAsync(hostName));
 
@@ -98,6 +102,44 @@ private static async Task TestGetHostEntryAsync(Func<Task<IPHostEntry>> getHostE
             Assert.Equal<IPAddress>(list1, list2);
         }
 
+        public static bool GetHostEntry_DisableIPv6_Condition = GetHostEntryWorks && RemoteExecutor.IsSupported;
+
+        [ConditionalTheory(nameof(GetHostEntry_DisableIPv6_Condition))]
+        [InlineData("")]
+        [InlineData(TestSettings.LocalHost)]
+        public void Dns_GetHostEntry_DisableIPv6_ExcludesIPv6Addresses(string hostnameOuter)
+        {
+            RemoteExecutor.Invoke(RunTest, hostnameOuter).Dispose();
+
+            static void RunTest(string hostnameInner)
+            {
+                AppContext.SetSwitch("System.Net.DisableIPv6", true);
+                IPHostEntry entry = Dns.GetHostEntry(hostnameInner);
+                foreach (IPAddress address in entry.AddressList)
+                {
+                    Assert.NotEqual(AddressFamily.InterNetworkV6, address.AddressFamily);
+                }
+            }   
+        }
+
+        [ConditionalTheory(nameof(GetHostEntry_DisableIPv6_Condition))]
+        [InlineData("")]
+        [InlineData(TestSettings.LocalHost)]
+        public void Dns_GetHostEntryAsync_DisableIPv6_ExcludesIPv6Addresses(string hostnameOuter)
+        {
+            RemoteExecutor.Invoke(RunTest, hostnameOuter).Dispose();
+
+            static async Task RunTest(string hostnameInner)
+            {
+                AppContext.SetSwitch("System.Net.DisableIPv6", true);
+                IPHostEntry entry = await Dns.GetHostEntryAsync(hostnameInner);
+                foreach (IPAddress address in entry.AddressList)
+                {
+                    Assert.NotEqual(AddressFamily.InterNetworkV6, address.AddressFamily);
+                }
+            }
+        }
+
         [Fact]
         public async Task Dns_GetHostEntry_NullStringHost_Fail()
         {
diff --git a/src/libraries/System.Net.NameResolution/tests/PalTests/System.Net.NameResolution.Pal.Tests.csproj b/src/libraries/System.Net.NameResolution/tests/PalTests/System.Net.NameResolution.Pal.Tests.csproj
index 7b2b9ee2c9fd60..3b9a81c4ee5d35 100644
--- a/src/libraries/System.Net.NameResolution/tests/PalTests/System.Net.NameResolution.Pal.Tests.csproj
+++ b/src/libraries/System.Net.NameResolution/tests/PalTests/System.Net.NameResolution.Pal.Tests.csproj
@@ -28,6 +28,8 @@
              Link="Common\System\Net\IPEndPointStatics.cs" />
     <Compile Include="$(CommonPath)System\Net\IPAddressParserStatics.cs"
              Link="Common\System\Net\IPAddressParserStatics.cs" />
+    <Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.cs"
+             Link="Common\System\Net\SocketProtocolSupportPal.cs" />
     <Compile Include="$(CommonTestPath)System\Net\Configuration.cs"
              Link="Common\System\Net\Configuration.cs" />
     <Compile Include="$(CommonTestPath)System\Net\Configuration.Http.cs"
diff --git a/src/libraries/System.Net.Ping/src/System.Net.Ping.csproj b/src/libraries/System.Net.Ping/src/System.Net.Ping.csproj
index d3fd26f7079675..5687146f9d6aea 100644
--- a/src/libraries/System.Net.Ping/src/System.Net.Ping.csproj
+++ b/src/libraries/System.Net.Ping/src/System.Net.Ping.csproj
@@ -21,6 +21,8 @@
              Link="Common\System\Net\IPAddressParserStatics.cs" />
     <Compile Include="$(CommonPath)System\Net\SocketAddress.cs"
              Link="Common\System\Net\SocketAddress.cs" />
+    <Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.cs"
+             Link="Common\System\Net\SocketProtocolSupportPal.cs" />
     <!-- Logging -->
     <Compile Include="$(CommonPath)System\Net\Logging\NetEventSource.Common.cs"
              Link="Common\System\Net\Logging\NetEventSource.Common.cs" />
diff --git a/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj b/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj
index 35d10bb679f163..496c628956f8c1 100644
--- a/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj
+++ b/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj
@@ -65,6 +65,8 @@
              Link="Common\System\Net\SocketAddress.cs" />
     <Compile Include="$(CommonPath)System\Net\TcpValidationHelpers.cs"
              Link="Common\System\Net\TcpValidationHelpers.cs" />
+    <Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.cs"
+             Link="Common\System\Net\SocketProtocolSupportPal.cs" />
     <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
              Link="Common\System\Threading\Tasks\TaskToApm.cs" />
     <!-- System.Net.Internals -->
diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/OSSupport.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/OSSupport.cs
index 3e2886baf52b7e..9ed625aecd0ffc 100644
--- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/OSSupport.cs
+++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/OSSupport.cs
@@ -3,6 +3,7 @@
 
 
 using System.Threading;
+using Microsoft.DotNet.RemoteExecutor;
 using Xunit;
 
 namespace System.Net.Sockets.Tests
@@ -25,6 +26,37 @@ public void SupportsIPv6_MatchesOSSupportsIPv6()
 #pragma warning restore
         }
 
+        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
+        public void DisableIPv6_OSSupportsIPv6_False()
+        {
+            RemoteInvokeOptions options = new RemoteInvokeOptions();
+            options.StartInfo.EnvironmentVariables["DOTNET_SYSTEM_NET_DISABLEIPV6"] = "1";
+            RemoteExecutor.Invoke(RunTest, options).Dispose();
+
+            static void RunTest()
+            {
+                Assert.False(Socket.OSSupportsIPv6);
+            }
+        }
+
+        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
+        public void DisableIPv6_SocketConstructor_CreatesIPv4Socket()
+        {
+            RemoteExecutor.Invoke(RunTest).Dispose();
+
+            static void RunTest()
+            {
+                AppContext.SetSwitch("System.Net.DisableIPv6", true);
+                using Socket socket1 = new Socket(SocketType.Stream, ProtocolType.Tcp);
+                using Socket socket2 = new Socket(SocketType.Dgram, ProtocolType.Udp);
+
+                Assert.Equal(AddressFamily.InterNetwork, socket1.AddressFamily);
+                Assert.Equal(AddressFamily.InterNetwork, socket2.AddressFamily);
+                Assert.False(socket1.DualMode);
+                Assert.False(socket2.DualMode);
+            }
+        }
+
         [Fact]
         public void IOControl_FIONREAD_Success()
         {

From 62bbf444b913ca70f49dd9d5287eff5f082f459a Mon Sep 17 00:00:00 2001
From: Anton Firszov <antonfir@gmail.com>
Date: Thu, 1 Jul 2021 15:31:58 +0200
Subject: [PATCH 3/6] IncludeRemoteExecutor added by mistake

---
 .../src/System.Net.NameResolution.csproj                         | 1 -
 1 file changed, 1 deletion(-)

diff --git a/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj b/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj
index 26f359db8d0404..1642f1fff33dda 100644
--- a/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj
+++ b/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj
@@ -1,7 +1,6 @@
 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-    <IncludeRemoteExecutor>true</IncludeRemoteExecutor>
     <TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser</TargetFrameworks>
     <Nullable>enable</Nullable>
   </PropertyGroup>

From 82841b654b1ad18fcc80602a679d14bd2c71c7e7 Mon Sep 17 00:00:00 2001
From: Anton Firszov <antonfir@gmail.com>
Date: Thu, 1 Jul 2021 15:41:10 +0200
Subject: [PATCH 4/6] remove extra newline

---
 .../tests/FunctionalTests/GetHostEntryTest.cs                    | 1 -
 1 file changed, 1 deletion(-)

diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs
index 4b5a735f670750..8fd734f8b09c9c 100644
--- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs
+++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs
@@ -28,7 +28,6 @@ public async Task Dns_GetHostEntryAsync_IPAddress_Ok()
             // [ActiveIssue("https://github.com/dotnet/runtime/issues/51377", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
             !PlatformDetection.IsiOS && !PlatformDetection.IstvOS && !PlatformDetection.IsMacCatalyst;
 
-
         [ConditionalTheory(nameof(GetHostEntryWorks))]
         [InlineData("")]
         [InlineData(TestSettings.LocalHost)]

From d6b999a178d7959a05857e520dc7a05980812c70 Mon Sep 17 00:00:00 2001
From: Anton Firszov <antonfir@gmail.com>
Date: Thu, 1 Jul 2021 17:30:42 +0200
Subject: [PATCH 5/6] review suggestion

---
 .../src/System/Net/SocketProtocolSupportPal.cs     | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.cs b/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.cs
index 6e2642e00c271a..a61f47a0fa458d 100644
--- a/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.cs
+++ b/src/libraries/Common/src/System/Net/SocketProtocolSupportPal.cs
@@ -16,24 +16,18 @@ internal static partial class SocketProtocolSupportPal
 
         private static bool IsIPv6Disabled()
         {
-            bool value;
-
             // First check for the AppContext switch, giving it priority over the environment variable.
-            if (AppContext.TryGetSwitch(DisableIPv6AppCtxSwitch, out value))
+            if (AppContext.TryGetSwitch(DisableIPv6AppCtxSwitch, out bool disabled))
             {
-                return value;
+                return disabled;
             }
 
             // AppContext switch wasn't used. Check the environment variable.
             string? envVar = Environment.GetEnvironmentVariable(DisableIPv6EnvironmentVariable);
 
-            if (bool.TryParse(envVar, out value))
-            {
-                return value;
-            }
-            else if (uint.TryParse(envVar, out uint intVal))
+            if (envVar is not null)
             {
-                return intVal != 0;
+                return envVar == "1" || envVar.Equals("true", StringComparison.OrdinalIgnoreCase);
             }
 
             return false;

From a8f23cb88d26814144be6ebed0c7863a4c90b50d Mon Sep 17 00:00:00 2001
From: Anton Firszov <antonfir@gmail.com>
Date: Thu, 1 Jul 2021 18:32:53 +0200
Subject: [PATCH 6/6] watch SocketProtocolSupportPal.OSSupportsIPv6 in
 NameResolutionPal.Unix

---
 .../src/System/Net/NameResolutionPal.Unix.cs                | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs
index 46858edea5dfab..74c62684a82c5b 100644
--- a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs
+++ b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs
@@ -76,9 +76,11 @@ private static unsafe void ParseHostEntry(Interop.Sys.HostEntry hostEntry, bool
                     Interop.Sys.IPAddress* addressHandle = hostEntry.IPAddressList;
                     for (int i = 0; i < hostEntry.IPAddressCount; i++)
                     {
-                        if (Array.IndexOf(nativeAddresses, addressHandle[i], 0, nativeAddressCount) == -1)
+                        Interop.Sys.IPAddress nativeAddr = addressHandle[i];
+                        if (Array.IndexOf(nativeAddresses, nativeAddr, 0, nativeAddressCount) == -1 &&
+                            (!nativeAddr.IsIPv6 || SocketProtocolSupportPal.OSSupportsIPv6)) // Do not include IPv6 addresses if IPV6 support is force-disabled
                         {
-                            nativeAddresses[nativeAddressCount++] = addressHandle[i];
+                            nativeAddresses[nativeAddressCount++] = nativeAddr;
                         }
                     }
 
