public async Task Run() { while (!disposed) { try { logger.info("starting connection..."); AsyncSocket tempSocket = null; foreach (var telegramEndpoint in dc.Endpoints) { logger.debug("connecting to {}:{}...", telegramEndpoint.Host, telegramEndpoint.Port); try { tempSocket = new AsyncSocket(); await tempSocket.Connect(telegramEndpoint.Host, telegramEndpoint.Port); logger.debug("connect success"); break; } catch (TelegramSocketException e) { logger.info("connect to {0}:{1} error: {2}", telegramEndpoint.Host, telegramEndpoint.Port, e); tempSocket = null; } } if (tempSocket == null) { logger.warning("connection failed... wait 2 seconds and retry"); await Task.Delay(TimeSpan.FromSeconds(3)); continue; } lock (this) { socket = tempSocket; sendCounter = 0; } logger.debug("send connected event..."); OnConnectedEvent(); input.Clear(); while (!disposed) { logger.debug("reading new chunk..."); input.AddChunk(await socket.Read()); } } catch (Exception e) { logger.info("connection error: {0}", e); } lock (this) { socket = null; } logger.debug("disconnected"); OnDisconnectedEvent(); } }