Can you help us with an intermittent socket connect exception in our game?

Hello everyone,
Our game uses Unity SDK.
For the matchmaking and the notifications we have one connected socket.
When the socket is closed by error during the game we are trying to recover and connect new socket,
but we are not trying to recover when socket.ConnectAsync throws exception.
So some of our users, when trying to connect socket are getting exception errors:

  • System.AggregateException: One or more errors occurred. —>System.IO.IOException: Authentication f

  • System.Net.Sockets.SocketException (0x80004005): Access denied
    at System.Net.Sockets.SocketAsyncRe

Do you familiar with them?
Do you think that we should try to reconnect new socket also at that situation?

Hey @uri it looks like you are connecting your socket without checking to see if your session token is still valid. Try doing a session.IsExpired check at the beginning of your game to reauthenticate if your token is invalid. Then connect with the socket.

@lugehorsam Thanks for the response.
At the beginning of our game, we always do a new authenticate and getting a new session by using the client.AuthenticateCustomAsync or client.AuthenticateFacebookAsync.
The exceptions that I said before could also happen pretty soon after the authenticate is complete but not to everyone.

@uri could you share a snippet in this thread of the code you are using to connect? The server is returning this error because you are passing an invalid session to the connect method.

@lugehorsam sharing the code that connects the socket.


Our session is a field of the class and I know for sure that a new session was initialized before we try to connect the socket.

Okay thanks. Like I said before, are you sure that the session has not expired before you connect? You mentioned that it can happen during a socket reconnect, so it’s possible that the socket is losing connection and then you are trying to reconnect with a stale session from your initial authenticate.

Could you also share your full exception messages? They are cutoff a bit from what you shared.

Hey, I know that we do authenticate just before connecting the socket.
For Example, the user does client.AuthenticateCustomAsync() at 11:54:52 and at 11:56:03 he tries to connect the socket and we got the AggregateException.
We set the session token_expiry_sec to be 10 hours.

Unfortunately, I only got 100 chars from the exception messages but I found in our unity cloud log for the same exception.

  • System.AggregateException: One or more errors occurred. —> System.Net.Sockets.SocketException: Access denied at System.Net.Sockets.SocketAsyncResult.CheckIfThrowDelayedException () [0x00000] in <00000000000000000000000000000000>:0 at System.Net.Sockets.TcpClient.EndConnect (System.IAsyncResult asyncResult) [0x00000] in <00000000000000000000000000000000>:0 at System.Action1[T].Invoke (T obj) [0x00000] in <00000000000000000000000000000000>:0 at System.Threading.Tasks.TaskFactory1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func2[T,TResult] endFunction, System.Action1[T] endAction, System.Threading.Tasks.Task1[TResult] promise, System.Boolean requiresSynchronization) [0x00000] in <00000000000000000000000000000000>:0 at System.Threading.Tasks.TaskFactory1[TResult].FromAsyncImpl[TArg1,TArg2] (System.Func5[T1,T2,T3,T4,TResult] beginMethod, System.Func2[T,TResult] endFunction, System.Action`1[T] endAction, TArg1 arg1, TArg2 arg2, System.Object state, System.Threading.Tasks.Ta

I’d check your server and infrastructure logs but the server is clearly rejecting the socket reconnect. If the session isn’t invalid then it must be an issue with how you are hosting Nakama. I’d recommend looking at the server logs and/or looking at Heroic Cloud as a hosting option.

our server is hosted on Heroic Cloud.

Okay, could you share your server logs for when this occurs?

We didn’t get any server logs for when this occurs.

Hi @uri welcome the the Heroic Labs and Nakama community. I rewored your topic title and moved it to the Game Design category to make it easier for others to follow and contribute.

@url okay, I think the issue may be that you are attempting to reconnect while the socket is still connected.

How are you determining that the socket connection has dropped in order to reconnect? Are you using the Closed or ReceivedError events?

yes, I am doing reconnect to the socket by listening to the Close even.
also, I am checking it again before reconnecting the socket for if the socket.IsConnected = false.

Okay thanks @uri, I will write a few tests to see if I can reproduce this behavior. Is it happening only on a particular platform? Or in the editor?

it happens on both android and ios.

Okay @uri this is a normal TCP reset that occurs as users move onto different connections. I’d try to reconnect in this situation.