private void handleRequest()
        {
            StreamWriter wCli = null;
            StreamReader rCli = null;

            try
            {
                NetworkStream strLig = ligCli.GetStream();
                wCli = new StreamWriter(strLig);
                rCli = new StreamReader(strLig);

                wCli.AutoFlush = true;

                string data=null;
                data = rCli.ReadLine();

                //System.Windows.Forms.MessageBox.Show(data);

                //char[] _data = data.ToCharArray();
                // Here we should receive a GETKEY urn:mpeg:mpeg21:diid:doi:pt/adetti/music/00000005
                // Here we shoudl receive a GETKEY MD5HASH

                // we should return the key, if the license hasn't expired yet (playcount)
                //System.Windows.Forms.MessageBox.Show(data.Substring(0,6));
                if(String.Compare(data.Substring(0,6),"GETKEY")==0 || String.Compare(data.Substring(0,6),"getkey")==0) // This is a valid request
                {
                    char[] _data = data.ToCharArray(); // convert to chars to extract the image ID and level

                    int n=0;
                    string CID = null;
                    for(n=7; n<data.Length; n++)
                    {
                        if(_data[n]!=' ')
                            CID += _data[n];
                    }

                    //System.Windows.Forms.MessageBox.Show(":"+CID+":");

                    // now we have the content id

                    if (CID==null)
                    {
                        wCli.Write("ERROR");
                        System.Windows.Forms.MessageBox.Show("The content identifier is empty!!!");
                    }
                    else
                    {

                        // the wallet needs to do the following:
                        // 1. Check wheter a license for that image is already on the system

                        if(checkLicenseOnSystem(CID))
                        {
                            // ok, we already have the license on our system
                            // we need to read the license from the file
                            string license = null;
                            CUser cu = new CUser();
                            if(!cu.readUserData(this.uname, this.pwd))
                            {
                                wCli.Write("ERROR");
                                System.Windows.Forms.MessageBox.Show("An error has occured while trying to read the data file.");
                            }
                            else
                            {
                                if(readLicenseFromSystem(CID, this.uname, this.pwd, ref license))
                                {
                                    string key = null;
                                    if(checkLicenseValidityPlaycount(CID))
                                    {
                                        if(getRequestedKey(license, ref key))
                                        {
                                            wCli.WriteLine(key);
                                        }
                                        else
                                        {
                                            wCli.Write("ERROR");
                                        }
                                    }
                                    else
                                    {
                                        // delete current license file
                                        deleteLicenseFile(CID);
                                        // try to get a new one from the license server ????
                                        wCli.Write("ERROR");
                                    }
                                }
                                else
                                {
                                    wCli.Write("ERROR");
                                }
                            }
                        }
                        else // we don't have the license file, we need to obtain it remotely from License Server
                        {
                            string license = null;
                            CUser cu = new CUser();
                            if(!cu.readUserData(this.uname, this.pwd))
                            {
                                wCli.Write("ERROR");
                                System.Windows.Forms.MessageBox.Show("An error has occured while trying to read the data file.");
                            }
                            else
                            {
                                //System.Windows.Forms.MessageBox.Show(cu.getUid());
                                if(obtainLicenseFromWS(cu.getUid(), CID, this.uname, this.pwd, ref license))
                                {
                                    //System.Windows.Forms.MessageBox.Show(license);
                                    if(createLicenseFile(CID, this.uname, this.pwd, license))
                                    {
                                        string key = null;
                                        if(checkLicenseValidityPlaycount(CID))
                                        {
                                            if(getRequestedKey(license, ref key))
                                            {
                                                wCli.WriteLine(key);
                                            }
                                            else
                                            {
                                                wCli.Write("ERROR");
                                            }
                                        }
                                        else
                                        {
                                            wCli.Write("ERROR");
                                        }
                                    }
                                    else
                                    {
                                        wCli.Write("ERROR");
                                    }
                                }
                                else
                                {
                                    wCli.Write("ERROR");
                                }
                            }
                        }
                    }

                    //wCli.WriteLine("RES_LVL="+RES_LVL);
                    //wCli.WriteLine("id_imagem="+IMG_ID);
                }
                else // return ERROR
                {
                    wCli.Write("ERROR");
                }

                //System.Windows.Forms.MessageBox.Show(data);
            }
            catch (Exception e)
            {
                wCli.Write("ERROR");
                System.Windows.Forms.MessageBox.Show(e.Message);
                System.Windows.Forms.MessageBox.Show(e.StackTrace);
            }
            finally
            {
                if(wCli!=null)
                    wCli.Close();
                if(rCli!=null)
                    rCli.Close();
                if(ligCli!=null)
                    ligCli.Close();
            }
        }
        public bool readLicenseFromSystem(string cid, string username, string password, ref string rlicense)
        {
            string fname=cid.Replace("/","");
            string filename=fname.Replace(":","");
            FileStream fs = new FileStream(@".\data\"+filename, FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            CUser cu = new CUser();
            rlicense = cu.decipherDataS(username,password,sr.ReadLine());

            sr.Close();

            return true;
        }
        public bool createLicenseFile(string cid, string uname, string pwd, string license)
        {
            string fname=cid.Replace("/","");
            string filename=fname.Replace(":","");
            FileStream fs = new FileStream(@".\data\"+filename, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);

            CUser cu = new CUser();
            sw.WriteLine(cu.cipherDataS(uname, pwd, license));

            sw.Close();

            /** create the appropriate values in the registry */
            RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"Software\OpenSDRMWallet", true);
            rk.CreateSubKey(filename);
            rk.Close();

            rk = Registry.CurrentUser.OpenSubKey(@"Software\OpenSDRMWallet\"+filename, true);
            rk.SetValue("playcount", "0");
            rk.Close();

            /** now we need to parse the license and get the playcount */
            string pc = getPCFromLicense(license);

            /** insert the original playcount on the Registry */
            rk = Registry.CurrentUser.OpenSubKey(@"Software\OpenSDRMWallet\"+filename, true);
            rk.SetValue("playcount", pc);
            rk.Close();

            return true;
        }
        public osdrmWltMainForm()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //

            RegistryKey rk = Registry.CurrentUser.CreateSubKey(@"Software\OpenSDRMWallet");
            //rk.CreateSubKey(@"licenses");
            rk.Close();

            //RegistryKey rkey = null;
            //rkey = Registry.CurrentUser.OpenSubKey(@"Software\OpenSDRMWallet", true);
            //key.SetValue("teste", "123");
            //rkey.Close();

            // Validate if the User has or not yet the data files created
            // if yes, the buttons and textboxes are disabled and only registration of user is possible
            // else disable the registration button
            CUser user = new CUser();
            //menuItem1.Enabled=false;
            if(user.checkDataFiles()==false) // disable buttons
            {
                usernameTxt.Enabled = false;
                passwordTxt.Enabled = false;
                startWalletBtn.Enabled = false;
                registerUserBtn.Enabled = true;

                menuItem5.Enabled=true;
            }
            else
            {
                usernameTxt.Enabled = true;
                passwordTxt.Enabled = true;
                startWalletBtn.Enabled = true;
                registerUserBtn.Enabled = false;

                menuItem5.Enabled=false;
            }

            is_running = false;
        }
        private void startWalletBtn_Click(object sender, System.EventArgs e)
        {
            if(is_running == false)
            {
                CUser user = new CUser();
                if(user.validateLogin(usernameTxt.Text, passwordTxt.Text))
                {
                    //MessageBox.Show("Username is valid!!!");

                    uname = usernameTxt.Text;
                    pwd = passwordTxt.Text;

                    usernameTxt.Text = "";
                    usernameTxt.Enabled = false;
                    passwordTxt.Text = "";
                    passwordTxt.Enabled = false;
                    startWalletBtn.Text = "Terminate Wallet!!!";

                    //menuItem1.Enabled = true;
                    menuItem7.Enabled = false;
                    menuItem5.Enabled = false;

                    statusLabel.Text = "Wallet server is running...";
                    notifyIcon1.Text = "OSDRM Wallet - Wallet server is running...";

                    is_running = true;

                    goSrv = new Thread(new ThreadStart(this.goServer));
                    goSrv.IsBackground = true;
                    goSrv.Start();

                    WindowState = FormWindowState.Minimized;
                }
                else
                {
                    MessageBox.Show("Username is invalid!!!");
                }
            }
            else if(is_running == true)
            {
                //if(goSrv.IsAlive)
                //MessageBox.Show("estou a tentar matar a thread");
                goSrv.Interrupt();
                goSrv.Abort();

                //TcpClient cli = new TcpClient("127.0.0.1", 9999);
                //cli.Close();

                //this.stopServer();
                //goSrv.Abort();

                this.Dispose();
                Application.Exit();
            }
        }
        private void registerBtn_Click(object sender, System.EventArgs e)
        {
            CUser user = new CUser();
            string msg=null;
            if(user.validateUserData(usernameTxt.Text, passwordTxt.Text, repasswordTxt.Text, renameTxt.Text, addressTxt.Text, emailTxt.Text, ref msg)==false)
            {
                MessageBox.Show(msg);
            }
            else
            {
                user.setUserData(usernameTxt.Text, passwordTxt.Text, renameTxt.Text, addressTxt.Text, emailTxt.Text);
                if(!user.registerUserWS())
                {
                    MessageBox.Show("An error occured while registering the user on the Authentication Server!!!");

                    this.Dispose();
                } else if(user.writeUserData())
                {
                    MessageBox.Show("The registration was created successfully!!!");

                    mf.registerUserBtn.Enabled = false;
                    mf.usernameTxt.Enabled =  true;
                    mf.passwordTxt.Enabled = true;
                    mf.startWalletBtn.Enabled = true;

                    this.Dispose();
                }
            }
        }