public async Task Send(ChunkedStream message, CancellationToken token) { var length = message.Length; ArraySegment <byte> chunk; while (length > 0 && (chunk = message.StartRead()).Count > 0) { length -= chunk.Count; if (length < 0) { throw new IndexOutOfRangeException(); } using (var timeout = new CancellationTokenSource(SEND_TIMEOUT_MS)) using (var ctx = CancellationTokenSource.CreateLinkedTokenSource(token, timeout.Token)) await _socket.SendAsync(chunk, WebSocketMessageType.Binary, length == 0, ctx.Token); message.EndRead(chunk, chunk.Count); } if (length != 0) { throw new InvalidOperationException(); } }