private void cmdDisconnect_Click(object sender, EventArgs e)//If user wants to disconnect their team
        {
            byte[] buffer     = new byte[1024];
            string teamName   = txtTeamName.Text;
            string teamID     = txtTeamID.Text;
            string message    = MessageBuilder.unRegisterTeam(teamName, teamID);//Disconnecting unregisters team
            string ipString   = txtRegistryIP.Text;
            string portString = txtRegistryPort.Text;

            string[]       response = null;
            bool           isOK     = false;
            frmConnectTeam frm      = new frmConnectTeam();//Begin building form for reconnection
            IPAddress      ip       = IPAddress.Parse(ipString);
            int            port     = int.Parse(portString);
            Socket         regSock  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);//Create socket for the registry

            //Try to unregister team
            try
            {
                regSock.Connect(ip, port);//Connect to registry
                Logging.LogLine("Calling SOA-Registry with message :");
                Logging.LogLine("\t" + message);
                TCPHelper.sendMessage(message, regSock);                        //Send the disconnect messge
                string respMessage = TCPHelper.receiveMessage(buffer, regSock); //Wait for response
                Logging.LogLine("\tResponse from SOA-Registry:");
                Logging.LogLine("\t\t" + respMessage);
                Logging.LogLine("---");

                response = MessageParser.parseMessage(respMessage);//Parse response by |
                //Was the response OK?
                isOK = MessageParser.checkOK(response[1]);
                if (isOK == true)
                {
                    //Close current form and open new form
                    this.Hide();
                    frm.Show();
                    regSock.Disconnect(true);
                }
                else if (isOK == false)
                {
                    MessageBox.Show("ERROR CODE: " + response[2] + "\n" + response[3]);
                    regSock.Disconnect(true);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void cmdDisconnectTeam_Click(object sender, EventArgs e)
        {
            byte[] buffer     = new byte[1024];
            string teamName   = txtTeamName.Text;
            string teamID     = txtTeamID.Text;
            string message    = MessageBuilder.unRegisterTeam(teamName, teamID);
            string ipString   = txtRegIP.Text;
            string portString = txtRegPort.Text;

            string[]       response = null;
            bool           isOK     = false;
            frmConnectTeam frm      = new frmConnectTeam();
            IPAddress      ip       = IPAddress.Parse(ipString);
            int            port     = int.Parse(portString);
            Socket         regSock  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

            try
            {
                regSock.Connect(ip, port);
                Logging.LogLine("Calling SOA-Registry with message :");
                Logging.LogLine("\t" + message);
                TCPHelper.sendMessage(message, regSock);
                string respMessage = TCPHelper.receiveMessage(buffer, regSock);
                Logging.LogLine("\tResponse from SOA-Registry:");
                Logging.LogLine("\t\t" + respMessage);
                Logging.LogLine("---");
                response = MessageParser.parseMessage(respMessage);
                isOK     = MessageParser.checkOK(response[1]);
                if (isOK == true)
                {
                    frm.Show();
                    this.Hide();
                    regSock.Disconnect(true);
                }
                else if (isOK == false)
                {
                    MessageBox.Show("ERROR CODE: " + response[2] + "\n" + response[3]);
                    regSock.Disconnect(true);
                }
            }
            catch
            {
            }
        }
        private void cmdSelect_Click(object sender, EventArgs e)//Button for user to confirm selection of a service
        {
            byte[] buffer          = new byte[1024];
            string serviceSelected = cboServiceSelect.Text;
            string teamName        = txtTeamName.Text;
            string teamID          = txtTeamID.Text;
            string ipString        = txtRegIP.Text;
            string portString      = txtRegPort.Text;
            string expiration      = txtExpiration.Text;
            string mchInfo         = "";

            frmConnectTeam     frmConnect = new frmConnectTeam();
            frmSelectedService frmService = new frmSelectedService();

            string[] response = null;
            string[] args     = null;
            string[] rsps     = null;
            string   srvInfo  = "";

            string[] srvInfoParsed      = null;
            string[] mchInfoParsed      = null;
            bool     isOK               = false;
            int      regPort            = int.Parse(portString);
            int      numArgs            = 0;
            int      servicePort        = 0;
            int      numResponses       = 0;
            string   serviceIPString    = "";
            string   servicePortString  = "";
            string   serviceTeamName    = "";
            string   serviceName        = "";
            string   numArgsString      = "";
            string   serviceDescription = "";
            string   numResponsesString = "";
            //Build messages and connect sockets for communication
            string    tag       = MessageParser.parseTag(serviceSelected);
            string    message   = MessageBuilder.queryService(teamName, teamID, tag);//Build message to find a service
            IPAddress regIP     = IPAddress.Parse(ipString);
            IPAddress serviceIP = null;
            //Declare sockets for registry and service
            Socket regSock     = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            Socket serviceSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

            try
            {
                regSock.Connect(regIP, regPort);//Connect to the registry socket
                Logging.LogLine("Calling SOA-Registry with message :");
                Logging.LogLine("\t" + message);
                TCPHelper.sendMessage(message, regSock);                        //Send query service HL7 /message
                string respMessage = TCPHelper.receiveMessage(buffer, regSock); //Wait for response
                Logging.LogLine("\tResponse from SOA-Registry:");
                Logging.LogLine("\t\t" + respMessage);
                Logging.LogLine("---");
                response = MessageParser.parseMessage(respMessage); //parse response into deliminated parts
                isOK     = MessageParser.checkOK(response[1]);      //Check if OK or not
                if (isOK == true)
                {
                    args          = MessageParser.argsParser(MessageParser.parseMessageByEOS(respMessage));        //Get an array of arguments
                    rsps          = MessageParser.respParser(MessageParser.parseMessageByEOS(respMessage));        //get an array of responses
                    srvInfo       = MessageParser.parseSegment(SRV, MessageParser.parseMessageByEOS(respMessage)); //get the SRV segment...
                    srvInfoParsed = MessageParser.parseMessage(srvInfo);                                           //...Parse the SRV segment by | character
                    mchInfo       = MessageParser.parseSegment(MCH, MessageParser.parseMessageByEOS(respMessage)); //Get the MCH Segment...
                    mchInfoParsed = MessageParser.parseMessage(mchInfo);                                           //...parse the mch segment by | character
                    //Service connection info
                    serviceIPString   = mchInfoParsed[1];
                    servicePortString = mchInfoParsed[2];
                    serviceIP         = IPAddress.Parse(serviceIPString);
                    servicePort       = int.Parse(servicePortString);
                    serviceTeamName   = srvInfoParsed[1];
                    serviceName       = srvInfoParsed[2];
                    //Parse arguments and responses
                    numArgsString      = srvInfoParsed[4];
                    numArgs            = int.Parse(numArgsString);
                    numResponsesString = srvInfoParsed[5];
                    numResponses       = int.Parse(numResponsesString);
                    serviceDescription = srvInfoParsed[6];
                    //Build the form programmatically
                    FormBuilder.buildSelectedService(args, rsps, serviceName, serviceTeamName,
                                                     serviceDescription, serviceIPString, servicePortString,
                                                     teamName, teamID, expiration, ipString, portString, frmService);
                    this.Hide();
                }
                else if (isOK == false)
                {
                    MessageBox.Show("ERROR CODE: " + response[2] + "\n" + response[3]);
                    if (response[3].Contains("Team Licence Expired"))
                    {
                        frmConnect.Show();
                        this.Hide();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void cmdConnect_Click(object sender, EventArgs e) //Button user clicks when trying to connect to the registry
        {
            byte[]    buffer          = new byte[1024];           //data buffer for storing messages
            string    ipAddressString = txtHostName.Text;         //pull registry ip address from user input
            string    portString      = txtPort.Text;             //pull registry port from user input
            string    teamName        = txtTeamName.Text;         //pull team name from user input
            string    message         = "";                       //string for storing received messages
            string    teamMessage     = "";                       //string for storing HL7 register team message
            IPAddress ip    = null;
            bool      valid = true;
            bool      isOK  = false;
            int       port  = 0;

            Socket regSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); //Socket for the registry

            string[]            response   = null;                                                       //message received broken into parsed segments by |
            frmServiceSelection frm        = new frmServiceSelection();                                  //Next form as an object for building
            frmConnectTeam      frmConnect = new frmConnectTeam();

            try
            {
                port = int.Parse(portString);            //turn port from string to int
                ip   = IPAddress.Parse(ipAddressString); //turn ip from string to IPAddress
            }
            catch (Exception ex)
            {
                valid = false;
                MessageBox.Show(ex.Message);
            }
            if (valid == true)
            {
                try
                {
                    regSock.Connect(ip, port);                           //Connect to the registry
                    //Build register team message
                    teamMessage = MessageBuilder.registerTeam(teamName); //Build the Register Team message
                    Logging.LogLine("Calling SOA-Registry with message :");
                    Logging.LogLine("\t" + teamMessage);
                    TCPHelper.sendMessage(teamMessage, regSock);
                    message = TCPHelper.receiveMessage(buffer, regSock);
                    Logging.LogLine("\tResponse from SOA-Registry :");
                    Logging.LogLine("\t\t" + message);
                    Logging.LogLine("---");

                    response = MessageParser.parseMessage(message);
                    isOK     = MessageParser.checkOK(response[1]);
                    //Check if the respnse is an OK or NOT-OK response
                    if (isOK == true)
                    {
                        FormBuilder.buildServiceSelection(teamName, response[2], response[3], ipAddressString, portString, frm); //pull out the new form
                        this.Hide();                                                                                             //put this form away
                    }
                    else if (isOK == false)
                    {
                        MessageBox.Show("ERROR CODE: " + response[2] + "\n" + response[3]);
                        if (response[3].Contains("Team Licence Expired"))
                        {
                            frmConnect.Show();
                            this.Hide();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("ERROR:" + ex.Message);
                }
            }
        }