private void OnSend(IAsyncResult ar) { lock (sendLock) { if (isdisconnecting) { return; } int n; try { n = sock.EndSend(ar); totalSentBytes += n; if (UploadThrottled) { currentSentBytes += n; throttle.UpdateUpload(n); } else { currentUpSpeed = CalculateUpBps(); currentSentBytes = n; lastSendTime = DateTime.Now; } var buf = (ByteBuffer)ar.AsyncState; int r = buf.length - n; if (r > 0) { buf.offset = buf.bytes.Length - r; SendNow(buf); } else if (sendQueue.Count > 0) { SendNow(sendQueue.Dequeue()); } else { writing = false; if (closing) { DisconnectNow(false); } } } catch (ObjectDisposedException) { // socket has been closed return; } catch { DisconnectNow(true); return; } SentNotify(n, writing); } }