public void StartListen()
        {
            StopListen(); // if there is any channel still open --> close it

            try
            {
                int s32_Port = int.Parse(textBoxPort.Text);

                mi_Channel   = new TcpChannel(s32_Port);
                ChannelServices.RegisterChannel(mi_Channel);

                mi_Transfer  = new Remote.cTransfer();
                mi_Service = RemotingServices.Marshal(mi_Transfer, "TestService");

                // define the event which is triggered when the Client calls the CallServer() function
                mi_Transfer.ev_ServerCall += new Remote.cTransfer.del_ServerCall(OnClientEvent);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(this, "Error starting listening:\n" + Ex.Message, "Server");
                checkBoxListen.Checked = false; // calls StopListen()
            }
        }
        private void OnBtnSendClick(object sender, System.EventArgs e)
        {
            Remote.kAction k_Action = new Remote.kAction();

            k_Action.frictionL = int.Parse(data1.Text);
            k_Action.frictionR = int.Parse(data2.Text);
            k_Action.tempL = int.Parse(data3.Text);
            k_Action.tempR = int.Parse(data4.Text);
            k_Action.interiorTemp = int.Parse(data5.Text);
            k_Action.brake = int.Parse(data6.Text);

            if (k_Action.frictionL > 100) k_Action.frictionL = 100;
            else if (k_Action.frictionL < 0) k_Action.frictionL = 0;
            if (k_Action.frictionR > 100) k_Action.frictionR = 100;
            else if (k_Action.frictionR < 0) k_Action.frictionR = 0;
            if (k_Action.tempL > 100) k_Action.tempL = 100;
            else if (k_Action.tempL < 0) k_Action.tempL = 0;
            if (k_Action.tempR > 100) k_Action.tempR = 100;
            else if (k_Action.tempR < 0) k_Action.tempR = 0;
            if (k_Action.interiorTemp > 100) k_Action.interiorTemp = 100;
            else if (k_Action.interiorTemp < 0) k_Action.interiorTemp = 0;
            if (k_Action.brake > 10) k_Action.brake = 10;
            else if (k_Action.brake < 0) k_Action.brake = 0;

            this.Cursor = Cursors.WaitCursor;

            string s_URL = string.Format("tcp://{0}:{1}/TestService", textBoxComputer.Text, textBoxPort.Text);

            try
            {
                mi_Transfer = (Remote.cTransfer) Activator.GetObject(typeof(Remote.cTransfer), s_URL);

                // triggers the event mi_Transfer.ev_ServerCall in the Server
                Remote.kResponse k_Response = mi_Transfer.CallServer(k_Action);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(this, "Error sending message to Server:\n" + Ex.Message, "Client Error");
            }

            this.Cursor = Cursors.Arrow;
        }
        public void StopListen()
        {
            if (mi_Service != null)
                RemotingServices.Unmarshal (mi_Service);

            if (mi_Transfer != null)
                RemotingServices.Disconnect(mi_Transfer);

            if (mi_Channel != null)
                ChannelServices.UnregisterChannel(mi_Channel);

            mi_Service  = null;
            mi_Transfer = null;
            mi_Channel  = null;
        }