private void handleSimServerRequests(MessageWrapper wrappedMessage, ConsoleTcpListenerArgs args)
        {
            //retreive actual message payload and handle the different message types
            switch (wrappedMessage.MessageType)
            {
                case MessageBase.MESSAGE_JOIN_REQUEST:
                    XMLSimJoinRequest message = (XMLSimJoinRequest)wrappedMessage.Message;
                    if (message.valid())
                    {
                        try
                        {
                            //accept the connection from the SA
                            XMLSimJoinAcceptance joinAccept = new XMLSimJoinAcceptance();
                            joinAccept.ParticipantId = message.ParticipantId; //echo back
                            joinAccept.ParticipantIp = message.ParticipantIp;
                            joinAccept.ElementId = SimulationManagementHelper.generateElementId();
                            joinAccept.RequestConfig = true;

                            //wrap XML message with DEM header and send out
                            MessageWrapper wrap = new MessageWrapper();
                            wrap.ApplicationId = _simConfig.APP_ID;
                            wrap.ApplicationRole = _simConfig.APP_ROLE;
                            wrap.MessageType = MessageBase.MESSAGE_JOIN_ACCEPTANCE;
                            wrap.Message = joinAccept;

                            System.Console.WriteLine("Sending: \n" + wrap.toXmlString());
                            byte[] data = Encoding.UTF8.GetBytes(wrap.toXmlString());
                            NetworkStream netstream;
                            netstream = args.Client.GetStream();
                            netstream.Write(data, 0, data.Length);

                            String incoming = String.Empty;

                            bool done = false;
                            System.Console.WriteLine("Waiting for SA ");
                            //wait for the config from the SA
                            while (!done)
                            {
                                netstream = args.Client.GetStream();
                                if (netstream.DataAvailable && netstream != null)
                                {
                                    data = new Byte[args.Client.ReceiveBufferSize];
                                    netstream.Read(data, 0, (int)args.Client.ReceiveBufferSize);
                                    incoming = Encoding.UTF8.GetString(data);
                                    //System.Console.WriteLine("Received Data"+incoming);

                                    XmlSerializer deserializer = new XmlSerializer(typeof(MessageWrapper));
                                    MessageWrapper incomingWrappedMessage;

                                    //deserialize
                                    using (TextReader reader = new StringReader(incoming))
                                    {
                                        incomingWrappedMessage = (MessageWrapper)deserializer.Deserialize(reader);
                                    }

                                    //accepted to join the simulation, send config
                                    if (String.Equals(incomingWrappedMessage.MessageType, MessageBase.MESSAGE_SIM_APP_CONFIG))
                                    {
                                        System.Console.WriteLine("Received:" + incomingWrappedMessage.toXmlString());
                                        XMLSimApplication current = (XMLSimApplication)incomingWrappedMessage.Message;
                                        //add the SA to the list

                                        SimulationParticipant participant = new SimulationParticipant(current,new NetworkConnectionState(args.Client));

                                        _participantList.Add(current.ParticipantId, participant);
                                        done = true;

                                        if (_participantList.Values.Count > 1)
                                        {
                                            List<KeyValuePair<string,SimulationParticipant>> allParticipants = _participantList.ToList();
                                            List<XMLSimApplication> allApplications = new List<XMLSimApplication>();

                                            //build a list of applications to update the new app that joined
                                            foreach (var app in allParticipants)
                                            {
                                                allApplications.Add(app.Value.SimulationApplication);
                                            }

                                            XMLParticipantList updatedList = new XMLParticipantList();
                                            updatedList.Participants = allApplications;

                                            //wrap XML message with DEM header and send out
                                            wrap = new MessageWrapper();
                                            wrap.ApplicationId = _simConfig.APP_ID;
                                            wrap.ApplicationRole = _simConfig.APP_ROLE;
                                            wrap.MessageType = MessageBase.MESSAGE_UPDATE_PARTICPANT_LIST;
                                            wrap.Message = updatedList;

                                            //notifyParticipants(wrap);//tell everybody
                                        }
                                    }
                                }

                            }//end while

                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                        finally
                        {
                            args.Stream.Close();
                            args.Client.Close();
                        }
                    }
                    break;
            }
        }
        //This SA will act as a Simulation Manager
        public void CreateSimulation(object sender, RoutedEventArgs e)
        {
            String simId = Sim_ID.Text;
            String simName = Sim_Name.Text;

            if(simId.Length > SIM_ID_MAX_LENGTH || simId.Length < SIM_ID_MIN_LENGTH){
                MessageBox.Show("Simulation ID requires at least 5-36 characters"); return;
            }

            if (simName.Length > SIM_NAME_MAX_LENGTH)
            {
                MessageBox.Show("Simulation Name must be less than 50 characters"); return;
            }
            else if (simName == "") Sim_Name.Text = simName = "Cyber Threat Simulation #" + simId;

            //start a sim manager
            _simConfig.APP_ROLE = SimulationRunningConfig.ROLE_MANAGER;
            _simConfig.SIM_ID = simId;
            _simConfig.SIM_NAME = simName;

            if(Sim_Description.Text == "")
                _simConfig.SIM_DESCRIPTION = "None";
            else
                _simConfig.SIM_DESCRIPTION = Sim_Description.Text;

            AppRoleTextBox.Text = "ROLE: MANAGER";

            //start a new simulation as the simulation manager
            _simManager = new SimulationManagementHelper(_simConfig, DATA_SOURCE_STRING);
            _simManager.startBroadcastingSimulation();

            //add manager to list

            //generate this managers sim application config and add to participant list
            XMLSimApplication saConfig = new XMLSimApplication();
            saConfig.ElementId = SimulationManagementHelper.generateElementId();
            saConfig.ParticipantId = _simConfig.APP_ID;
            saConfig.ParticipantIp = _simConfig.LOCAL_IP_ADDRESS;
            saConfig.ParticipantRole = _simConfig.APP_ROLE;
            saConfig.SystemProfile = _simConfig._systemProfile;
            saConfig.loadSimApplicationConfiguration(_simConfig);
            saConfig.Status = "Listening";

            //Note that the NetworkConnectionState is null because the manager will not be communicating with itself
            //In addition, SA should not communicate with each other unless sending through the UDP network, they only
            //communicate with the manager at the time of connection setup or rejoin if disconnected.
            SimulationParticipant manager = new SimulationParticipant(saConfig, new NetworkConnectionState());
            _participantList.Add(saConfig.ParticipantId, manager);

            startSimulationServer();
        }