/// <summary>
        /// Add a subscriber callback to this connection. There can be many subscribers.
        /// </summary>
        /// <typeparam name="Tmsg">Message type used in the callback</typeparam>
        /// <param name="sub">Subscriber</param>
        /// <param name="callback">Method to call when a message matching the given subscriber is received</param>
        public RosBridgeSubscriber <Tmsg> Subscribe <Tmsg>(string topic, RosMessageCallback <Tmsg> callback, uint queueSize = 0) where Tmsg : RosMessage, new()
        {
            MessageCallback MessageCallback = (RosMessage msg) =>
            {
                Tmsg message = msg as Tmsg;
                callback(message);
            };

            var getMessageType = typeof(Tmsg).GetMethod("GetMessageType");

            if (getMessageType == null)
            {
                Debug.LogError("Could not retrieve method GetMessageType() from " + typeof(Tmsg).ToString());
                return(null);
            }
            string messageType = (string)getMessageType.Invoke(null, null);

            if (messageType == null)
            {
                Debug.LogError("Could not retrieve valid message type from " + typeof(Tmsg).ToString());
                return(null);
            }

            RosBridgeSubscriber <Tmsg> subscriber = new RosBridgeSubscriber <Tmsg>(topic, messageType);

            this.subscribers.Add(subscriber, MessageCallback);
            this.msgQueue.Add(subscriber.Topic, new RenderQueue <MessageTask>(queueSize));

            if (this.IsConnected)
            {
                this.webSocket.Send(RosBridgeMsg.Subscribe(subscriber.Topic, subscriber.Type));
            }

            return(subscriber);
        }
        /// <summary>
        /// Connect to the remote ros environment.
        /// </summary>
        public void Connect <Tmsg>() where Tmsg : RosMessage
        {
            if (this.IsConnected)
            {
                return;
            }

            string url = "ws://" + this.host + ":" + this.port;

            if (!CanConnect(url))
            {
                throw new Exception("Cannot connect. url=" + url);
            }

            this.webSocket = new WebSocket(url);

            this.webSocket.OnOpen    += (sender, eventArgs) => { Debug.Log("WebSocket Open  url=" + url); };
            this.webSocket.OnMessage += (sender, eventArgs) => this.OnMessage <Tmsg>(eventArgs.Data);
            this.webSocket.OnError   += (sender, eventArgs) => { Debug.LogError("WebSocket Error Message: " + eventArgs.Message); };
            this.webSocket.OnClose   += (sender, eventArgs) => this.OnClose();

//			this.webSocket.Connect();
            this.webSocket.ConnectAsync();

            DateTime startTime = DateTime.Now;

            while (this.webSocket.ReadyState != WebSocketState.Open)
            {
                if ((DateTime.Now - startTime).TotalMilliseconds > ConnectionTimeOut)
                {
                    AddCannotConnectUrlList(url);

                    SIGVerseLogger.Error("Failed to connect. IP=" + this.host + ", Port=" + this.port + "  (Time out)");
                    throw new Exception("Failed to connect. IP=" + this.host + ", Port=" + this.port + "  (Time out)");
                }

                Thread.Sleep(100);
            }

            if (!this.webSocket.IsAlive)
            {
                Debug.Log("Error: Connection was faild.");
            }
            else
            {
                Debug.Log("Connected to ROSbridge server");

                foreach (var sub in this.subscribers)
                {
                    this.webSocket.Send(RosBridgeMsg.Subscribe(sub.Key.Topic, sub.Key.Type));
                    Debug.Log("Sending: " + RosBridgeMsg.Subscribe(sub.Key.Topic, sub.Key.Type));
                }
                foreach (RosBridgePublisher pub in this.publishers)
                {
                    this.webSocket.Send(RosBridgeMsg.AdvertiseTopic(pub.Topic, pub.Type));
                    Debug.Log("Sending " + RosBridgeMsg.AdvertiseTopic(pub.Topic, pub.Type));
                }
                foreach (var srv in this.serviceProviders)
                {
                    this.webSocket.Send(RosBridgeMsg.AdvertiseService(srv.Key.Name, srv.Key.Type));
                    Debug.Log("Sending: " + RosBridgeMsg.AdvertiseService(srv.Key.Name, srv.Key.Type));
                }

                this.isConnected = true;

                //foreach (RosBridgePublisher pub in this.publishers)
                //{
                //	pub.CreatePublishingThread();
                //}
            }
        }