private CPResponseBase SendInternal(CPRequest request, CancellationToken ct, int timeout) { logger.Debug(request.ToString()); if (!IsConnected) { throw new Exception("Not connected"); } if (state != ClientState.Connected) { throw new Exception("Invalid state: " + state); } CPResponseBase response = null; InvokeCommand command = null; try { command = new InvokeCommand(request, (cSeq++)); StartCommand(command); bool cancel = false; ct.Register(() => { cancel = true; }); Func <bool> cancelled = () => (state != ClientState.Connected || cancel); response = command.WaitResult(timeout, cancelled) as CPResponseBase; } finally { if (command != null) { command.Dispose(); command = null; } } return(response); }