public async Task <bool> Link(Uri uri)
        {
            if (SockIO.GetState() == WebSocketState.Open)
            {
                return(true);
            }

            if (SockIO.GetState() != WebSocketState.Open && SockIO.GetState() != WebSocketState.Connecting)
            {
                await SockIO.ConnectAsync(uri);
            }

            if (SockIO.GetState() != WebSocketState.Open)
            {
                return(false);
            }

            SockIO.On("paired", HandlePairedResponse);
            SockIO.On("rekey", HandleRekeyResponse);
            SockIO.On("api", HandleApiResponse);
            SockIO.On("event", HandleEventResponse);

            StartTimeoutOpenTasksCheck();

            await Pair(true);

            return(true);
        }
        protected IEnumerator TimeoutOpenTasksCheck()
        {
            while (SockIO.GetState() == WebSocketState.Open)
            {
                var           now          = DateTime.Now;
                int           count        = 0;
                List <string> toRemoveKeys = new List <string>();

                foreach (var key in OpenTasks.Keys.ToList())
                {
                    OpenTask openTask;
                    if (!OpenTasks.TryGetValue(key, out openTask))
                    {
                        continue;
                    }

                    if ((now - openTask.TaskRequestTime).TotalMilliseconds >= openTask.TaskTimeoutMS)
                    {
                        toRemoveKeys.Add(key);
                    }

                    //sleep checking each 10 requests
                    if ((count % ScatterConstants.OPEN_TASK_NR_CHECK) == 0)
                    {
                        count = 0;
                        yield return(WaitForOpenTasksCheck(ScatterConstants.OPEN_TASK_CHECK_INTERVAL_SECS));
                    }

                    count++;
                }

                foreach (var key in toRemoveKeys)
                {
                    OpenTask openTask;
                    if (!OpenTasks.TryGetValue(key, out openTask))
                    {
                        continue;
                    }

                    OpenTasks.Remove(key);

                    openTask.PromiseTask.SetResult(BuildApiError());
                }
                yield return(WaitForOpenTasksCheck(ScatterConstants.OPEN_TASK_CHECK_INTERVAL_SECS));
            }
        }
 public bool IsConnected()
 {
     return(SockIO.GetState() == WebSocketState.Open);
 }