Getting error on android mobile device - An address incompatible with the requested protocol was used

Hi, How can I force web socket to use IPv4 instead of IPv6 in Unity Client as in some cases on mobile network, while connecting socket I’m getting this error- (on WIFI it works fine)

An address incompatible with the requested protocol was used, StackTrace =   at System.Net.Sockets.SocketAsyncResult.CheckIfThrowDelayedException () [0x00000] in <00000000000000000000000000000000>:0
08-12 12:40:35.560  3957  3974 E Unity   :   at System.Net.Sockets.TcpClient.EndConnect (System.IAsyncResult asyncResult) [0x00000] in <00000000000000000000000000000000>:0
08-12 12:40:35.560  3957  3974 E Unity   :   at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x00000] in <00000000000000000000000000000000>:0
08-12 12:40:35.560  3957  3974 E Unity   :   at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncImpl[TArg1,TArg2] (System.Func`5[T1,T2,T3,T4,TResult] beginMethod, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, TArg1 arg1, TArg2 arg2, System.Object state, System.Threading.Tasks.TaskCreationOptions creationOptions) [0x00000] in <0000000000

I googled about this issue An address incompatible with the requested protocol was used and found out that, this issue is due to IPv6.
Then for testing it out, I disabled the IPv6 on the mobile device and the error was gone and successfully connected to socket while being on mobile network.

So, how can I ensure WebSocket is using the correct protocol.

@hdjay0129 What version of Unity engine do you use? Is the mobile device on T-Mobile or Sprint networks?

Please try and add this line to before you initialize the client object in the SDK and see if it brings an improvement:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var client = new Client(scheme, host, port, serverKey);
// etc.

@novabyte, We are using Unity version 2018.4.3f1.
The mobile devices are on India’s vodafone carrier networks.
Tried on two different devices having vodafone 4G LTE and same issue & when changed the carrier’s APN Protocol to use IPv4 only instead of IPv4/IPv6, websocket successfully connected as seen in Logs on ManagedCloud.

I tried that with the new build containing the code you provided.

Interesting. Can you please reach out to us via email and provide the hostname so we can look into the project network setup for you?

@mofirouz sure.
Just sent a support email.

@hdjay0129 I’ve done some investigation into this error from Unity engine and tracked it down to this Mono runtime issue. Looks like it was patched 15 months ago but until Unity engine adopts the version I don’t think there’s a workaround. Sadly the entire patch is just 3 lines of code.

Is it possible you could update your Unity project to the latest 2019.x release? Before you do make sure to take a complete project backup because Unity has a habit of breaking more things than they fix in each release. This is not an issue with Nakama or the load balancer configuration.

Then let me know what version of Mono runtime is in use in the latest 2019 release you try with this code:

Type type = Type.GetType("Mono.Runtime");
if (type != null)
{
    MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
    if (displayName != null)
    {
        Debug.Log(displayName.Invoke(null, null));
    }
}