I have two C # programs that should communicate via WebSockets, and the server has been working fine (also using Alchemy) and for some time now (with javascript clients). However, an Alchemy WebSocketClient connection causes a NullReferenceException be NullReferenceException on the client side.
After some debugging, I found that the exception was thrown on line 187 of WebSocketClient.cs due to a call to _context.Connection.Connected . _context.Connection is the same instance as the local _client , and none of them are null at this point in time. The TcpClient.Connected page on msdn.microsoft.com lists possible exceptions (indicating that it should not throw any exceptions).
Going back, I noticed that the variable of the _client class _client changed in line 176 of WebSocketClient.cs from a regular looking instance to an instance filled with exceptions:
BEFORE LINE 176 
AFTER LINE 176 
Immediately after this line, the Dispose () method for Context , which has just been added on line 187, is called Connection disconnecting (which is the same instance as _client that I mentioned earlier on line 131 Context.cs .
My question is: why is this happening and how can I stop it and successfully connect to the desired WebSocketServer?
If you need more information, please feel free to ask me in the comments.
EDIT 1
An exception occurs only when the server is actually listening, if the server is inactive on line 176 WebSocketClient.cs not reached (this is the correct behavior because OnRunClient() is called when creating a connection to the server).
EDIT 2
As requested by StackTrace:
System.NullReferenceException was unhandled HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=System StackTrace: at System.Net.Sockets.TcpClient.get_Connected() at Alchemy.WebSocketClient.SetupContext(Context context) in C:\...\Alchemy\WebSocketClient.cs:regel 187 at Alchemy.WebSocketClient.HandleClientThread() in C:\...\Alchemy\WebSocketClient.cs:regel 94 at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: