private void listenForMessages() { //Listen for client messages while (this.mReading) { try { if (this.mMessageThread.IsAlive) { Thread.Sleep(TcpClientProxy.ReadTimeout); } } catch {} try { //Request a read for each connected clients lock (this.mClients) { foreach (DictionaryEntry entry in this.mClients) { //Validate client is still active (no session timeout) TcpClientProxy client = (TcpClientProxy)entry.Value; if (client.SessionTimedOut) { //Client timed-out; dispose the object this.mTimeoutDelegate.BeginInvoke(null, new ClientEventArgs(client), null, null); } else { //Check for a message from this client byte[] message = client.Read(); if (message != null) { this.mReceivedDelegate.BeginInvoke(null, new ClientMessageEventArgs(client, message), null, null); } } } } this.mMessageHeartbeatDelegate.BeginInvoke(this, EventArgs.Empty, null, null); } catch (Exception ex) { this.mReceiveErrorDelegate.BeginInvoke(this, new ErrorEventArgs(new ApplicationException("ArgixTcpListener::listenForMessages() exception.", ex)), null, null); } } }