static async Task Main(string[] args) { var gracefulShutdown = new CancellationTokenSource(); var sigterms = Observable.Create <bool>(observer => { Console.CancelKeyPress += (sender, eventArgs) => { observer.OnNext(false); eventArgs.Cancel = true; }; return(gracefulShutdown); }) .Do(x => gracefulShutdown.Cancel()); if (!Server.IsRunning()) { await StartAsServer(gracefulShutdown.Token, sigterms); } else { await StartAsClient(gracefulShutdown.Token, sigterms); } }