private void DoHandshake(Rfc6455Handshake handshake)
        {
            var buffer = new byte[1024];
            Socket.Send(Encoding.UTF8.GetBytes(handshake.ToString()), () => Socket.Receive(buffer, r =>
            {
                //check if only a partial handshake received (noticed with/ IIS8 hosts)
                if (r < 2)
                {
                    //read the rest of the handshake
                    ReadHandshakeContinue(buffer, r);
                }
                IsHandshakeDone = true;

                Receive();

                if (!this.IsPrimitive)
                {
                    BindUnboundBindings();
                }

                if (this.OnSocketOpen != null)
                    this.OnSocketOpen.Invoke(this, null);
            }, err => FireOnClose()),
                        err => FireOnClose());
        }
        private Task<bool> DoHandshake(Rfc6455Handshake handshake)
        {
            return new Task<bool>(() =>
            {
                Socket.Send(Encoding.UTF8.GetBytes(handshake.ToString()), () =>
                {
                    StartReceiving();
                }
                , exception =>
                {

                });

                return SpinWait.SpinUntil(() => IsHandshakeDone, ConnectionTimeout);
            });
        }
        private Task<bool> DoHandshake(Rfc6455Handshake handshake)
        {

            return new Task<bool>(() =>
            {
                var cts = new CancellationTokenSource();
                var token = cts.Token;

                var working = true;

                var buffer = new byte[1024];
                Socket.Send(Encoding.UTF8.GetBytes(handshake.ToString()), () => Socket.Receive(buffer, r =>
                {
                    Receive();
                    IsHandshakeDone = true;

                    foreach (var ctrl in this.Controllers.GetAll())
                    {
                        ctrl.BindUnboundSubscriptions();
                    }
                    working = false;
                }, err => FireOnDisconnected()),
                            err => FireOnDisconnected());


                while (working)
                {
                    if (token.IsCancellationRequested)
                        return false;
                    Thread.Sleep(1);
                }
                return true;
            });


        }