/// <summary>
 /// Convenience function to write full buffer data to the server.
 /// </summary>
 /// <param name="buffer">The buffer data to send.</param>
 /// <returns>Returns Task handle to the write operation.</returns>
 public Task WriteAsync(BinaryStream buffer)
 {
     return EnsureWriteAsync(buffer, _disposeToken.Token);
 }
 /// <summary>
 /// Write the buffer data to the server.
 /// </summary>
 /// <param name="buffer">The buffer data to send.</param>
 /// <param name="cancellationToken">A cancellation token which will cancel the request.</param>
 /// <returns>Returns Task handle to the write operation.</returns>
 public Task WriteAsync(BinaryStream buffer, CancellationToken cancellationToken)
 {
     return EnsureWriteAsync(buffer, cancellationToken);
 }
        private async Task EnsureWriteAsync(BinaryStream buffer, CancellationToken cancellationToken)
        {
            try
            {
                // Get the client first
                var client = await GetClientAsync();

                // We now need to flush asynchronously the stream
                var length = await buffer.Flush();

                // Now we can send the buffer.
                await client.GetStream()
                    .WriteAsync(buffer.GetBuffer(), 0, length, cancellationToken);
            }
            catch
            {
                if (_disposeToken.IsCancellationRequested) throw new ObjectDisposedException("Object is disposing.");
                throw;
            }
        }