public void receive()
        {
            String message = String.Empty;

            while (true)
            {
                NetworkStream netstream = client.GetStream();
                if (netstream.DataAvailable && netstream != null)
                {
                    byte[] data = new Byte[client.ReceiveBufferSize];
                    netstream.Read(data, 0, (int)client.ReceiveBufferSize);
                    message = Encoding.UTF8.GetString(data);

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

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

                    //accepted to join the simulation, send config
                    if (String.Equals(wrappedMessage.MessageType, MessageBase.MESSAGE_JOIN_ACCEPTANCE))
                    {
                        System.Console.WriteLine("Received: \n" + wrappedMessage.toXmlString());

                        XMLSimJoinAcceptance acceptance = (XMLSimJoinAcceptance) wrappedMessage.Message;

                        //generate and send this SA config
                        XMLSimApplication saConfig = new XMLSimApplication();
                        saConfig.ElementId = acceptance.ElementId;
                        saConfig.ParticipantId = _appConfig.APP_ID;
                        saConfig.ParticipantIp = _appConfig.LOCAL_IP_ADDRESS;
                        saConfig.ParticipantRole = _appConfig.APP_ROLE;
                        saConfig.SystemProfile = _appConfig._systemProfile;
                        saConfig.loadSimApplicationConfiguration(_appConfig);
                        saConfig.Status = "Waiting";

                        MessageWrapper wrapper = generateMessageWrapper(MessageBase.MESSAGE_SIM_APP_CONFIG);
                        wrapper.Message = saConfig;

                        //send sim manager this SA config
                        System.Console.WriteLine("Sending: \n"+ wrapper.toXmlString());
                        send(wrapper.toXmlString());
                        while (true)
                        {
                            //needs implementation -- waiting to start Simulation, set cyber terrain model, etc.
                        }
                    }

                    //received update participant list notification
                    else if (String.Equals(wrappedMessage.MessageType, MessageBase.MESSAGE_UPDATE_PARTICPANT_LIST))
                    {

                    }
                }

            }//end while
        }
        //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();
        }
 public SimulationParticipant(XMLSimApplication app, NetworkConnectionState socketState)
 {
     _simApp = app;
     _statefulSocket = socketState;
 }