private void ListenerThreadSync() { if (_streamReader is null) { _logger?.LogWarning("StreamReader was not initialized. Listener thread cannot be started."); return; } _logger?.LogInformation("Listener thread created and started."); try { while (true) { try { var message = _streamReader.ReadLine() ?? throw new IOException("Socket closed."); _logger?.LogDebug(message); _messageHandler.OnMessage(message); } catch (IOException exception) { _messageHandler.Close("Connection Terminated.", exception); return; } catch (Exception exception) { _logger?.LogDebug(exception, "Something went wrong on the Listener Thread."); } } } catch (ThreadInterruptedException exception) { _logger?.LogDebug(exception, "The Listener Thread was interrupted."); } }