public Task Close() { var taskCompletionSource = new TaskCompletionSource <bool>(); if (_client.State == CommunicationState.Closed || _client.State == CommunicationState.Closing) { taskCompletionSource.TrySetResult(false); } else { _client.CloseAsync(taskCompletionSource); } return(taskCompletionSource.Task); }