//private void _WebsocketClient_MessageReceived(object sender, Net.WebSocketClientMessageEventArgs e) {
        //	string data = e.Message;
        //	if (!string.IsNullOrWhiteSpace(data) && data.StartsWith("{")) {
        //		CommandReceived?.Invoke(this, new JsonCommandEventArgs(new JsonCommand(data)));
        //	}
        //}

        //public void Start() {
        //	if (WebSocketClient != null)
        //		WebSocketClient.Open();
        //}
        /// <summary>
        /// Führt einen JsonCommand aus. Dies kann entweder über eine TCP-Verbindung oder über eine URL (Webservice) geschehen
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public async Task <JsonCommandRetValue> DoCommand(JsonCommand cmd)
        {
            // sicherstellen, daß der User und der Token immer mitgeschickt werden
            cmd.SetParameter("user", this.Username);
            cmd.SetParameter("token", this.Token);
            JsonCommandRetValue retval = await MakeUrlRequest(cmd);

            // Wir haben ja jetzt einen Auth-Service eingebaut, damit nicht jeder Hinz und Kunz hier JsonCommands absetzen kann ;-)
            // Daher muss ich jetzt die 401-Meldungen abfangen
            if (retval.ReturnCode == 401)
            {
                // 401 = unauthorisiert, nun einfach einen login-command senden, und dann prüfen, ob dann der command durchgeht
                JsonCommand authCommand = new JsonCommand("", "Login");
                authCommand.SetParameter("user", Username);
                authCommand.SetParameter("password", _Password);
                JsonCommandRetValue authCommandRetValue = await MakeUrlRequest(authCommand);

                if (authCommandRetValue.ReturnCode == 200 || authCommandRetValue.ReturnCode == 1)
                {
                    // Login erfolgreich, also wieder den alten JsonCommand senden
                    this.Token = authCommandRetValue.ReturnValue;
                    cmd.SetParameter("token", this.Token);
                    return(await MakeUrlRequest(cmd));
                }
                else
                {
                    // Login fehlgeschlagen, ich gebe den LoginRetValue zurück
                    return(authCommandRetValue);
                }
            }
            else
            {
                return(retval);
            }
        }
        public async Task <JsonCommandRetValue> DoRawCommand(string cmdData)
        {
            using (var client = new HttpClient()) {
                //client.BaseAddress = new Uri(CommandUrl);
                //client.DefaultRequestHeaders.Accept.Clear();
                //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var stringContent = new StringContent(cmdData, Encoding.UTF8, "application/json");                      // Utils.Security.Cryptography.EncryptStringAES(cmd.ToJson().ToString()));
                //stringContent.Headers.Add("Content-Type", "application/json");
                //stringContent.Headers.Add("X-JsonCommand", cmd.CommandName);
                //var stream = File.OpenRead("B:\\test3.pdf");
                //HttpResponseMessage response = await client.PostAsync(CommandUrl, new StreamContent(stream));
                //stream.Close();
                //Utils.IO.IOUtil.StringToFile("C:\\temp\\post.dat", await stringContent.ReadAsStringAsync());
                HttpResponseMessage response = await client.PostAsync(CommandUrl, stringContent);

                if (response.IsSuccessStatusCode)
                {
                    if (response.Content != null)
                    {
                        string json = await response.Content.ReadAsStringAsync();

                        //if (!string.IsNullOrWhiteSpace(json))
                        //	json = Utils.Security.Cryptography.DecryptStringAES(json);
                        JsonCommandRetValue retval = new JsonCommandRetValue(json);
                        //retval.ReturnValue = json;
                        return(retval);
                    }
                }
                else
                {
                    //Console.WriteLine("Failed to publish: HTTP " + response.StatusCode);
                    if (response.Content != null)
                    {
                        string json = await response.Content.ReadAsStringAsync();

                        JsonCommandRetValue retval = new JsonCommandRetValue(-1, "Failed to publish: HTTP " + response.StatusCode);
                        retval.ReturnValue = json;
                        return(retval);
                    }
                }
            }
            return(new JsonCommandRetValue());
        }
        private async Task <JsonCommandRetValue> MakeUrlRequest(JsonCommand cmd)
        {
            try {
                using (var client = new HttpClient()) {
                    var stringContent = new StringContent(JsonConvert.SerializeObject(cmd), Encoding.UTF8, "application/json");                          // Utils.Security.Cryptography.EncryptStringAES(cmd.ToJson().ToString()));
                    stringContent.Headers.Add("X-JsonCommand", cmd.CommandName);

                    HttpResponseMessage response = await client.PostAsync(CommandUrl, stringContent);

                    if (response.IsSuccessStatusCode)
                    {
                        if (response.Content != null)
                        {
                            string json = await response.Content.ReadAsStringAsync();

                            JsonCommandRetValue retval = new JsonCommandRetValue(json);
                            return(retval);
                        }
                    }
                    else
                    {
                        //Console.WriteLine("Failed to publish: HTTP " + response.StatusCode);
                        if (response.Content != null)
                        {
                            string json = await response.Content.ReadAsStringAsync();

                            JsonCommandRetValue retval = new JsonCommandRetValue(-1, "Failed to publish: HTTP " + response.StatusCode);
                            retval.ReturnValue = json;
                            return(retval);
                        }
                    }
                }
            } catch (Exception ex) {
                string s = ex.Message + "\r\n" + ex.StackTrace;
                return(new JsonCommandRetValue(-1, "Fehler bei JsonCommandClient.MakeUrlRequest: " + ex.ToString(true)));
            }
            return(new JsonCommandRetValue());
        }