/// <summary>
        /// Puts new values into xively feed.
        /// </summary>
        /// <param name="ids">The ids.</param>
        /// <param name="values">The values.</param>
        /// <exception cref="System.ApplicationException">The xively server returned invalid status code.</exception>
        public void Put(string[] ids, object[] values)
        {
            var datastreams = new Hashtable[ids.Length];
            for (int i = 0; i < ids.Length; i++)
                datastreams[i] = new Hashtable
                                    {
                                        {"id",ids[i]},
                                        {"current_value",values[i]},
                                    };

            var json = new Hashtable
            {
                {"version","1.0.0"},
                {"datastreams",datastreams},
            };


            var request = new HttpRequest
            {
                Method = "PUT",
                Url = url,
                Content = JSON.JsonEncode(json),
            };

            request.AddHeader("X-ApiKey", apiKey);

            var response = httpClient.ExecuteRequest(request);

            if (response.StatusCode != 200)
                throw new ApplicationException("Xively server returned invalid status code : " + response.StatusCode);
        }
        public object Call(string method, params object[] arguments)
        {
            var message = new Hashtable
            {
                {"H",Hub},
                {"M",method},
                {"A",arguments},
                {"I",counter++},
            };
            var json = JSON.JsonEncode(message);

            var request = new HttpRequest
            {
                Url = Url + "send?transport=longPolling&connectionToken=" + ConnectionToken,              
                Method = "POST",
                Accept = "application/json",
                ContentType = "application/x-www-form-urlencoded; charset=UTF-8",
                Content = "data=" + HttpClient.UrlEncode(json),
            };
            var response = httpClient.ExecuteRequest(request);

            return JSON.JsonDecode(response.Content);
        }
        private void Negociate()
        {
            var random = new Random();
            var id = random.NextDouble();

            var request = new HttpRequest
            {
                Url = Url + "negotiate?_=" + id,
                Accept = "application/json",
            };

            var response = httpClient.ExecuteRequest(request);
            if (response.StatusCode != HttpStatusCode.OK)
                return;


            int startIndex = response.Content.IndexOf('{');
            int endIndex = response.Content.LastIndexOf('}');
            var json = response.Content.Substring(startIndex, endIndex - startIndex+1);
            var parameters = (Hashtable)JSON.JsonDecode(json);
            ConnectionToken = parameters["ConnectionToken"] as string;
            ConnectionId = parameters["ConnectionId"] as string;
        }
        object CallConnect()
        {
            var json = JSON.JsonEncode(
                new ArrayList()
                    {
                        new Hashtable { { "name", client.Hub } }
                    }
            );
            var encoded = HttpClient.UrlEncode(json);

            var request = new HttpRequest
            {
                Url = client.Url
                        + "connect?transport=longPolling&connectionToken=" + client.ConnectionToken
                        + "&connectionData=" + encoded,
                Accept = "application/json",
            };
            var response = httpClient.ExecuteRequest(request);
            if (response.StatusCode != HttpStatusCode.OK)
                return null;

            Debug.Print("!! " + response.Content);
            var data = JSON.JsonDecode(response.Content) as Hashtable;
            return data;
        }