public IDisposable Connect()
        {
            var disposables = new CompositeDisposable();

            var outgoingMessages = mProfilerControl.GetControlMessages()
                                   .StartWith(cStartSendingInstrumentationEvents)
                                   .TakeUntilDisposed(disposables)
                                   .Select(msg => msg.ToByteArray())
                                   .MonitorSubscriptionCount(out var whenOutgoingMessagesSubscriptionCountChanges);

            ProfilerCommunication.CreateRawChannel(mPipeName, outgoingMessages)
            .Take(1)     // for now just deal with the first connection
            .Subscribe(stream => mIncomingRawMessageStreams.OnNext(stream));

            disposables.Add(ModelUpdateSource.Connect());

            return(disposables);
        }
        private Connection(string pipeName, IProfilerControl profilerControl)
        {
            mPipeName        = pipeName;
            mProfilerControl = profilerControl;

            mIncomingRawMessageStreams = new BehaviorSubject <IObservable <byte[]> >(Observable.Empty <byte[]>());
            var whenConnected = new Subject <bool>();

            var incomingMessages = mIncomingRawMessageStreams
                                   .Select(stream => Observable.Defer(() =>
            {
                whenConnected.OnNext(true);
                return(stream.Finally(() => whenConnected.OnNext(false)));
            }))
                                   .Switch()
                                   .Select(Protocol.EventMessage.Parser.ParseFrom);

            ModelUpdateSource = new ModelUpdateSource(incomingMessages);
            WhenConnected     = whenConnected.Publish(false).ConnectForEver();
        }