/// <summary>
        /// Send out the broadcast and parse the return string
        /// </summary>
        /// <returns>True if an ARE has been detected, otherwise false</returns>
        public bool Detect(MainWindow window)
        {
            Dictionary<string, string> foundedAREs = new Dictionary<string, string>();

            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) {
                IPInterfaceProperties ipProps = nic.GetIPProperties();
                // check if localAddr is in ipProps.UnicastAddresses
                if (nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback) {

                    System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                    byte[] byteString = enc.GetBytes("AsTeRICS Broadcast");
                    UdpClient udpSend = null;

                    try {
                        udpSend = new UdpClient(receivePort, AddressFamily.InterNetwork);

                        udpSend.MulticastLoopback = true;
                        udpSend.EnableBroadcast = true;
                        foreach (MulticastIPAddressInformation ipInfo in ipProps.MulticastAddresses) {
                            Console.WriteLine(ipInfo.Address);
                            if (ipInfo.Address.AddressFamily == AddressFamily.InterNetwork) {
                                udpSend.JoinMulticastGroup(ipInfo.Address);
                                break;
                            }
                        }

                        IPEndPoint groupEp = new IPEndPoint(IPAddress.Broadcast, sendPort);
                        udpSend.Connect(groupEp);

                        udpSend.Send(byteString, byteString.Length);
                        Thread.Sleep(50);  // Sleeps are needed to give the system the time to close the ports.
                        // Otherwise confilicts because of still open ports
                        udpSend.Close();
                        Thread.Sleep(50);

                        IPEndPoint recvEp = new IPEndPoint(IPAddress.Any, 0);
                        UdpClient udpResponse = new UdpClient(receivePort);
                        //Loop to give the ARE some time to respond
                        for (int i = 0; i <= 10; i++) {
                            if (udpResponse.Available > 0) {
                                IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, receivePort);

                                Byte[] recvBytes = udpResponse.Receive(ref recvEp);
                                //Console.WriteLine("Rxed " + recvBytes.Length + " bytes, " + enc.GetString(recvBytes));
                                String retString = enc.GetString(recvBytes);
                                if (retString.Contains("AsTeRICS Broadcast Ret")) {
                                    hostname = retString.Substring(33, retString.IndexOf(" IP:") - 33); // Parsing the hostname out f the return string
                                    ipAdd = retString.Substring(retString.IndexOf(" IP:") + 4, retString.Length - retString.IndexOf(" IP:") - 4);
                                    if (!foundedAREs.ContainsKey(hostname)) {
                                        foundedAREs.Add(hostname, ipAdd);
                                    }
                                }

                                //break;
                            }
                            else {
                                //Console.WriteLine("nothing received");
                                Thread.Sleep(200); // wait before the next attemt to read data from the port
                            }
                        }
                        Thread.Sleep(100);
                        udpResponse.Close();
                    }
                    catch (Exception ex) {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(ex.StackTrace);
                        //throw ex;
                    }
                }
            }

            // if more than one ARE is detected, a selection dialog will be opend
            if (foundedAREs.Count > 1) {

                storageDialog = new StorageDialog();

                foreach (string s in foundedAREs.Keys) {
                    storageDialog.filenameListbox.Items.Add(s + " (" + foundedAREs[s] + ")");
                }
                storageDialog.filenameListbox.SelectionChanged += filenameListbox_SelectionChanged;
                storageDialog.Title = Properties.Resources.MultipleAREsDialogTitle;
                storageDialog.filenameTextbox.Text = foundedAREs.Keys.First();

                storageDialog.listLabel.Content = Properties.Resources.MultipleAREsDialogMsg;
                storageDialog.modelNameLabel.Content = Properties.Resources.MultipleAREsDialogSelected;
                storageDialog.cancelButton.Visibility = Visibility.Hidden;
                storageDialog.filenameTextbox.IsEnabled = false;

                storageDialog.Owner = window;
                storageDialog.ShowDialog();

                if (storageDialog.filenameTextbox.Text != null && storageDialog.filenameTextbox.Text != "") {
                    hostname = storageDialog.filenameTextbox.Text;
                    ipAdd = foundedAREs[hostname];
                }

            }
            if (ipAdd.Equals("")) {
                return false;
            }
            else {
                return true;
            }
        }
        /// <summary>
        /// Create an ARE GUI element, needed for older models, to update them to the current version
        /// </summary>
        /// <param name="mw">The MainWindow</param>
        /// <param name="deployModel">Deployment Motel, containing all components of the model</param>
        public static void UpdateToCurrentVersion(MainWindow mw, model deployModel)
        {
            if (deployModel.version != model.VERSION) {
                if (deployModel.version == "20120301" || deployModel.version == "20120509" || deployModel.version == "20111104") {
                    // From version 20120301 to 20120509, only minor changes without any change in the older deployment files are made
                    // 20111104 should also work, needs further tests!!!

                    //Update GUI components resolution from, 1/100 to 1/10000
                    foreach (componentType comp in deployModel.components) {
                        if (comp.gui != null) {
                            comp.gui.height = String.Concat(comp.gui.height, "00");
                            comp.gui.width = String.Concat(comp.gui.width, "00");
                            comp.gui.posX = String.Concat(comp.gui.posX, "00");
                            comp.gui.posY = String.Concat(comp.gui.posY, "00");
                        }
                    }

                    // Add the AREGUIWindow
                    deployModel.modelGUI = new modelGUIType();
                    deployModel.modelGUI.AREGUIWindow = new guiType();

                    deployModel.modelGUI.AREGUIWindow.height = "5000";
                    deployModel.modelGUI.AREGUIWindow.width = "9000";
                    deployModel.modelGUI.AREGUIWindow.posX = "0";
                    deployModel.modelGUI.AREGUIWindow.posY = "0";
                    deployModel.modelGUI.AlwaysOnTop = false;
                    deployModel.modelGUI.Decoration = true;
                    deployModel.modelGUI.Fullscreen = false;
                    deployModel.modelGUI.ShopControlPanel = true;
                    deployModel.modelGUI.ToSystemTray = false;

                    MessageBox.Show(Properties.Resources.UpdateModelVersionGUIInfoFormat(deployModel.version,model.VERSION), Properties.Resources.UpdateModelVersionGUIHeader, MessageBoxButton.OK, MessageBoxImage.Information);

                    deployModel.version = model.VERSION;
                    mw.ModelHasBeenEdited = true;
                }
            }
        }
        public OptionsDialog(MainWindow mainWindow)
        {
            InitializeComponent();

            // set the LanguageCombo to the currently used language/culture:
            this.mainWindow = mainWindow;
            String lang;
            if (ACS.Properties.Resources.Culture != null)
                lang = ACS.Properties.Resources.Culture.Name;
            else
                lang = "en-GB";
            switch (lang) {
                case "en-GB":
                    LanguageCombo.SelectedIndex = 0;
                    break;
                case "de-AT":
                    LanguageCombo.SelectedIndex = 1;
                    break;
                case "es-ES":
                    LanguageCombo.SelectedIndex = 2;
                    break;
                case "pl-PL":
                    LanguageCombo.SelectedIndex = 3;
                    break;
            }

            // set host and port to values from ini
            HostBox.Text = mainWindow.Ini.IniReadValue("ARE", "default_host");
            PortBox.Text = mainWindow.Ini.IniReadValue("ARE", "default_port");
            if (mainWindow.Ini.IniReadValue("ARE", "enable_autodetection").Equals("true")) {
                AutodetectARERadioButton1.IsChecked = true;
                HostBox.IsEnabled = false;
            } else {
                AutodetectARERadioButton2.IsChecked = true;
                HostBox.IsEnabled = true;
            }

            // read connection timeout setting
            ConnectionTimeoutBox.Text = mainWindow.Ini.IniReadValue("ARE", "socket_timeout");
            double timeoutSliderOut = 5000;
            Double.TryParse(mainWindow.Ini.IniReadValue("ARE", "socket_timeout"), out timeoutSliderOut);
            ConnectionTimeoutSlider.Value = timeoutSliderOut;

            // read and enable status polling
            if (mainWindow.Ini.IniReadValue("ARE", "enable_status_polling").Equals("true")) {
                EnableStatusUpdateCheckBox.IsChecked = true;
                StatusUpdateSlider.IsEnabled = true;
            } else {
                EnableStatusUpdateCheckBox.IsChecked = false;
                StatusUpdateSlider.IsEnabled = false;
            }
            StatusUpdateFrequencyBox.Text = mainWindow.Ini.IniReadValue("ARE", "status_polling_frequency");
            double updateSliderOut = 5000;
            Double.TryParse(mainWindow.Ini.IniReadValue("ARE", "status_polling_frequency"), out updateSliderOut);
            StatusUpdateSlider.Value = updateSliderOut;

            if (mainWindow.Ini.IniReadValue("Options", "createBackupFile").Equals("true")) {
                EnableAutomaticBackupCheckBox.IsChecked = true;
            } else {
                EnableAutomaticBackupCheckBox.IsChecked = false;
            }

            // set checkboxes to values from ini
            if (mainWindow.Ini.IniReadValue("Options", "showNamingDialogOnComponentInsert").Equals("true")) {
                NamingDialogCheckBox.IsChecked = true;
            } else {
                NamingDialogCheckBox.IsChecked = false;
            }
            if (mainWindow.Ini.IniReadValue("Options", "showHostPortDialogOnConnect").Equals("true")) {
                HostPortDialogCheckBox.IsChecked = true;
            } else {
                HostPortDialogCheckBox.IsChecked = false;
            }
            if (mainWindow.Ini.IniReadValue("Options", "showEventChannelConnectMessage").Equals("true")) {
                EventChannelMessageCheckBox.IsChecked = true;
            } else {
                EventChannelMessageCheckBox.IsChecked = false;
            }
            if (mainWindow.Ini.IniReadValue("Options", "showAREConnectedMessage").Equals("true")) {
                AREConnectedMessageCheckBox.IsChecked = true;
            } else {
                AREConnectedMessageCheckBox.IsChecked = false;
            }
            if (mainWindow.Ini.IniReadValue("Options", "showOverrideModelQuestion").Equals("true")) {
                OverrideModelQuestionCheckBox.IsChecked = true;
            } else {
                OverrideModelQuestionCheckBox.IsChecked = false;
            }
            if (mainWindow.Ini.IniReadValue("Options", "showOverrideLocalModelQuestion").Equals("true")) {
                OverrideLocalModelQuestionCheckBox.IsChecked = true;
            } else {
                OverrideLocalModelQuestionCheckBox.IsChecked = false;
            }
            if (mainWindow.Ini.IniReadValue("Options", "showOverrideLocalWhenConnected").Equals("true")) {
                OverrideModelFromAREAtConnectQuestionCheckBox.IsChecked = true;
            } else {
                OverrideModelFromAREAtConnectQuestionCheckBox.IsChecked = false;
            }
            if (mainWindow.Ini.IniReadValue("Options", "showOverrideAndRunLocalWhenConnected").Equals("true")) {
                OverrideAndRunFromAREAtConnectQuestionCheckBox.IsChecked = true;
            } else {
                OverrideAndRunFromAREAtConnectQuestionCheckBox.IsChecked = false;
            }
            if (mainWindow.Ini.IniReadValue("Options", "showOverrideComponentCollectionQuestion").Equals("true")) {
                OverrideComponentCollectionQuestionCheckBox.IsChecked = true;
            } else {
                OverrideComponentCollectionQuestionCheckBox.IsChecked = false;
            }

            // set the colors to the ones currently used (if no settings can be found in the ini, the default colors are used):
            BrushConverter bc = new BrushConverter();
            headerColor = mainWindow.Ini.IniReadValue("Layout", "headercolor");
            if (headerColor.Equals("")) headerColor = ACS.LayoutConstants.TOPRECTANGLECOLOR;
            groupColor = mainWindow.Ini.IniReadValue("Layout", "groupcolor");
            HeaderColorRectangle.Fill = (Brush)bc.ConvertFrom(headerColor);
            if (groupColor.Equals(""))
            groupColor = ACS.LayoutConstants.GROUPRECTANGLECOLOR;
            GroupColorRectangle.Fill = (Brush)bc.ConvertFrom(groupColor);
            bodyColor = mainWindow.Ini.IniReadValue("Layout", "bodycolor");
            if (bodyColor.Equals("")) bodyColor = ACS.LayoutConstants.MAINRECTANGLECOLOR;
            BodyColorRectangle.Fill = (Brush)bc.ConvertFrom(bodyColor);
            inPortColor = mainWindow.Ini.IniReadValue("Layout", "inportcolor");
            if (inPortColor.Equals("")) inPortColor = ACS.LayoutConstants.INPORTRECTANGLECOLOR;
            InPortColorRectangle.Fill = (Brush)bc.ConvertFrom(inPortColor);
            outPortColor = mainWindow.Ini.IniReadValue("Layout", "outportcolor");
            if (outPortColor.Equals("")) outPortColor = ACS.LayoutConstants.OUTPORTRECTANGLECOLOR;
            OutPortColorRectangle.Fill = (Brush)bc.ConvertFrom(outPortColor);
            eventInPortColor = mainWindow.Ini.IniReadValue("Layout", "eventinportcolor");
            if (eventInPortColor.Equals("")) eventInPortColor = ACS.LayoutConstants.EVENTINPORTCOLOR;
            EventInPortColorRectangle.Fill = (Brush)bc.ConvertFrom(eventInPortColor);
            eventOutPortColor = mainWindow.Ini.IniReadValue("Layout", "eventoutportcolor");
            if (eventOutPortColor.Equals("")) eventOutPortColor = ACS.LayoutConstants.EVENTOUTPORTCOLOR;
            EventOutPortColorRectangle.Fill = (Brush)bc.ConvertFrom(eventOutPortColor);

            //creationToolPathText.Text = mainWindow.Ini.IniReadValue("Options", "pathToPluginCreationTool");
            //activationToolPathText.Text = mainWindow.Ini.IniReadValue("Options", "pathToPluginActivationTool");
        }
 /// <summary>
 /// Create an ARE GUI element, needed for older models, to update them to the current version
 /// </summary>
 /// <param name="mw">The MainWindow</param>
 /// <param name="deployModel">Deployment Motel, containing all components of the model</param>
 /// <param name="componentList">List with all available components (plug-ins)</param>
 public static void UpdateMissingGUI(MainWindow mw, model deployModel, Hashtable componentList)
 {
     foreach (componentType comp in deployModel.components) {
         Asterics.ACS2.componentTypesComponentType compType = (Asterics.ACS2.componentTypesComponentType)componentList[comp.type_id];
         if (comp.gui == null && compType != null && compType.gui != null) {
             comp.gui = new guiType();
             comp.gui.height = compType.gui.height;
             comp.gui.width = compType.gui.width;
             comp.gui.posX = "0";
             comp.gui.posY = "0";
             mw.AddGUIComponent(comp);
             mw.ModelHasBeenEdited = true;
         }
     }
 }