/// <summary>
        /// Create SM session. Close if previously opened.
        /// </summary>
        /// <param name="uri">The URI</param>
        /// <param name="option">Options for session</param>
        /// <returns>The return code.</returns>
        private static int CreateSession(Uri uri, SMProtocolOptions option)
        {
            int res = 0;
            CloseSession();

            session = new SMSession(uri, false, option);

            // URI can still be invalid, missing protocol prefix for example
            try
            {
                session.Open();

                session.OnOpen += OnSessionOpened;
                session.OnClose += OnSessionClosed;
                session.OnError += OnSessionError;
                session.OnStreamOpened += OnStreamOpened;
                session.OnStreamClosed += OnStreamClosed;

                // wait until session changes its state
                // for high latency connections we cannot just start using this session
                sessionMonitorEvent.Reset();
                timeoutSessionMonitor = false;
                ThreadPool.QueueUserWorkItem(new WaitCallback(SessionMonitorProc), 0);
                sessionMonitorEvent.WaitOne(300000);
                timeoutSessionMonitor = true;
            }
            catch
            {
                SMLogger.LogError("Unable to open session for " + uri);
                res = 1;
            }

            return res;
        }
        public void IncorrectPortFailure()
        {
            ManualResetEvent eventRaised = new ManualResetEvent(false);
            Random rand = new Random(DateTime.Now.Millisecond);
            int random = rand.Next() % 1000;
            Uri uri;
            Uri.TryCreate(Host + random, UriKind.Absolute, out uri);

            if (_session != null && _session.State == SMSessionState.Opened)
                _session.End();

            _session = new SMSession(uri, false);
            bool isError = false;
            _session.OnError += (s, e) =>
                                    {
                                        isError = true;
                                    };

            SMProtocol smProtocol = _session.Protocol;
            smProtocol.OnError += (s, e) =>
            {
                isError = true;
                eventRaised.Set();
            };

            _session.Open();
            eventRaised.WaitOne();

            Assert.IsTrue(isError);
        }
 private static SMSession CreateSession(Uri uri)
 {
     _session = new SMSession(uri, false);
     _session.Open();
     return _session;
 }
        /// <summary>
        /// Create SM session. Close if previously opened.
        /// </summary>
        /// <param name="uri">The URI</param>
        private static void CreateSession(Uri uri)
        {
            CloseSession();

            session = new SMSession(uri, false);

            // URI can still be invalid, missing protocol prefix for example
            try
            {
                session.Open();

                session.OnOpen += OnSessionOpened;
                session.OnClose += OnSessionClosed;
                session.OnError += OnSessionError;
                session.OnStreamOpened += OnStreamOpened;
            }
            catch
            {
                SMLogger.LogError("Unable to open session for " + uri.ToString());
            }
        }