public void ShouldNotTryToReconnectWithIncorrectPortWhenReconnectionDisabled()
        {
            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");
            ManualResetEvent = new ManualResetEvent(false);
            Flag = false;


            var options = CreateOptions();
            options.Reconnection = false;
            var uri = "http://localhost:3940";

            var manager = new Manager(new Uri(uri), options);
            socket = manager.Socket("/invalid");

            manager.On(Manager.EVENT_RECONNECT_ATTEMPT, () =>
            {
                Flag = true;
            });

            manager.On(Manager.EVENT_CONNECT_ERROR, async () =>
            {
                // set a timeout to let reconnection possibly fire
                log.Info("EVENT_CONNECT_ERROR");
                await Task.Delay(1000);
                ManualResetEvent.Set();
            });

            ManualResetEvent.WaitOne();
            log.Info("after WaitOne");
            socket.Disconnect();
            manager.Close();
            Assert.False(Flag);     
        }
        public void ShouldTryToReconnectTwiceAndFailWhenRequestedTwoAttemptsWithIncorrectAddressAndReconnectEnabled()
        {
            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");
            ManualResetEvent = new ManualResetEvent(false);
            Flag = false;
            var reconnects = 0;

            var options = CreateOptions();
            options.Reconnection = true;
            options.ReconnectionAttempts = 2;
            options.ReconnectionDelay = 10;
            var uri = "http://localhost:3940";


            var manager = new Manager(new Uri(uri), options);
            socket = manager.Socket("/asd");

            manager.On(Manager.EVENT_RECONNECT_ATTEMPT, () =>
            {
                log.Info("EVENT_RECONNECT_ATTEMPT");
                reconnects++;
            });

            manager.On(Manager.EVENT_RECONNECT_FAILED, () =>
            {
                log.Info("EVENT_RECONNECT_FAILED");
                Flag = true;
                socket.Disconnect();
                manager.Close();
                ManualResetEvent.Set();
            });

            ManualResetEvent.WaitOne();
            Assert.Equal(2, reconnects);
        }
        public void ShouldTryToReconnectTwiceAndFailWhenRequestedTwoAttemptsWithImmediateTimeoutAndReconnectEnabled()
        {
            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");
            ManualResetEvent = new ManualResetEvent(false);
            Flag = false;
            var reconnects = 0;

            var options = CreateOptions();
            options.Reconnection = true;
            options.Timeout = 0;
            options.ReconnectionAttempts = 2;
            options.ReconnectionDelay = 10;            
            var uri = CreateUri();

            var manager = new Manager(new Uri(uri), options);

            manager.On(Manager.EVENT_RECONNECT_ATTEMPT, () =>
            {
                log.Info("EVENT_RECONNECT_ATTEMPT");
                reconnects++;
            });

            manager.On(Manager.EVENT_RECONNECT_FAILED, () =>
            {
                log.Info("EVENT_RECONNECT_FAILED");
                Flag = true;
                manager.Close();
                ManualResetEvent.Set();
            });

            ManualResetEvent.WaitOne();
            Assert.True(Flag);
            Assert.Equal(2,reconnects);
        }
        public void ShouldNotTryToReconnectAndShouldFormAConnectionWhenConnectingToCorrectPortWithDefaultTimeout()
        {
            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");
            ManualResetEvent = new ManualResetEvent(false);
            Flag = false;


            var options = CreateOptions();
            options.Reconnection = true;
            options.ReconnectionDelay = 10;
            var uri = CreateUri();

            var manager = new Manager(new Uri(uri), options);
            socket = manager.Socket("/valid");

            manager.On(Manager.EVENT_RECONNECT_ATTEMPT, () =>
            {
                Flag = true;
            });

            socket.On(Socket.EVENT_CONNECT, async() =>
            {
                // set a timeout to let reconnection possibly fire
                log.Info("EVENT_CONNECT");
                await Task.Delay(1000);

                ManualResetEvent.Set();
            });

            ManualResetEvent.WaitOne();
            log.Info("after WaitOne");
            socket.Close();
            manager.Close();
            Assert.False(Flag);                   
        }
        public async Task ShouldReconnectByDefault()
        {
            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");
            ManualResetEvent = new ManualResetEvent(false);
            Flag = false;

            var options = CreateOptions();
            var uri = CreateUri();

            var manager = new Manager(new Uri(uri), options);
            socket = manager.Socket("/");

            manager.On(Socket.EVENT_RECONNECT, () =>
            {
                log.Info("EVENT_RECONNECT");
                Flag = true;
                socket.Disconnect();
                ManualResetEvent.Set();
            });

            await Task.Delay(500);
            log.Info("before EngineSocket close");
            manager.EngineSocket.Close();

            ManualResetEvent.WaitOne();
            Assert.True(Flag);
        }