//Initialize MITes Receivers
        
        private bool InitializeMITes(string dataDirectory)
        {

                //for (int i = 0; (i < this.sensors.TotalReceivers); i++)
                foreach(Receiver receiver in this.sensors.Receivers)
                {
#if (PocketPC)
                    if (receiver.Type == SXML.Constants.RECEIVER_BLUETOOTH)
                    {
                        progressMessage += "Initializing Bluetooth ...";
                        this.bluetoothControllers[receiver.ID] = new BluetoothController();                     
                        try
                        {
                            //this.bluetoothControllers[i].initialize(this.configuration.MacAddress, this.configuration.Passkey);
                            this.bluetoothControllers[receiver.ID].initialize(receiver.MacAddress, receiver.PassKey);
                        }
                        catch (Exception e)
                        {
                            progressMessage += "Could not find a valid Bluetooth Wockets receiver!";
                            return false;
                        }
                        receiver.Running = true;

                        if (receiver.Decoder==SXML.Constants.DECODER_MITES)
                            this.mitesDecoders[receiver.ID] = new MITesDecoder();
                        else if (receiver.Decoder==SXML.Constants.DECODER_WOCKETS)
                            this.mitesDecoders[receiver.ID]= new MITesDecoder();
                        else if (receiver.Decoder == SXML.Constants.DECODER_SPARKFUN)
                            this.mitesDecoders[receiver.ID] = new MITesDecoder();
                    }
                    else 
#endif                        
                    if (receiver.Type == SXML.Constants.RECEIVER_USB)
                    {

                        progressMessage += "Searching for receiver " + receiver.ID + "...\r\n";
                        this.mitesControllers[receiver.ID] = new MITesReceiverController(MITesReceiverController.FIND_PORT, BYTES_BUFFER_SIZE);
                        int portNumber = MITesReceiverController.FIND_PORT;

                        //#if (PocketPC)


                        try
                        {
#if (PocketPC)
                            portNumber = 9;
                            progressMessage += "Testing COM Port " + portNumber;
                            if (this.mitesControllers[receiver.ID].TestPort(portNumber, BYTES_BUFFER_SIZE))
                            {
                                progressMessage += "... Success\r\n";
                            }
                            else
                            {
                                progressMessage += "... Failed\r\n";
                                portNumber = MITesReceiverController.FIND_PORT;
                            }
#else
                    for (int j = maxPortSearched; (j < Constants.MAX_PORT); j++)
                    {
                        portNumber = maxPortSearched = j;
                        progressMessage += "Testing COM Port " + portNumber;
                        if (this.mitesControllers[receiver.ID].TestPort(portNumber, BYTES_BUFFER_SIZE))
                        {
                            progressMessage += "... Success\r\n";
                            break;
                        }
                        else
                            progressMessage += "... Failed\r\n";
                    }
#endif
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Exiting: Could not find a valid COM port with a MITes receiver!");
                            for (int j = 0; (j < this.sensors.TotalReceivers); j++)
                                this.mitesControllers[j].Close();
#if (PocketPC)
                            Application.Exit();
                            System.Diagnostics.Process.GetCurrentProcess().Kill();
#else
                    Environment.Exit(0);
#endif
                        }


                        if (portNumber == MITesReceiverController.FIND_PORT)
                        {
                            progressMessage += "Could not find a valid COM port with a MITes receiver!";
                            //MessageBox.Show("Exiting: Could not find a valid COM port with a MITes receiver!");
#if (PocketPC)
                            //Application.Exit();
                            //System.Diagnostics.Process.GetCurrentProcess().Kill();    
#else
                    //Environment.Exit(0);
#endif
                            return false;
                        }
                        this.mitesControllers[receiver.ID].InitializeController(portNumber, BYTES_BUFFER_SIZE, true, MITesReceiverController.USE_THREADS);

                        this.mitesDecoders[receiver.ID] = new MITesDecoder();

                    }
                }
           // }

            aMITesActivityLogger = new MITesActivityLogger(dataDirectory + "\\data\\activity\\MITesActivityData");
            aMITesActivityLogger.SetupDirectories(dataDirectory);
            aMITesActivityCounters = new Hashtable();


            //aMITesPlotter = new MITesScalablePlotter(this.panel1, MITesScalablePlotter.DeviceTypes.IPAQ, maxPlots, this.mitesDecoders[0], GetGraphSize(false));

            //for each sensor created a counter
            for (int i = 0; (i < this.sensors.Sensors.Count); i++)
            {
                int sensor_id = Convert.ToInt32(((SXML.Sensor)this.sensors.Sensors[i]).ID);
                if (sensor_id != 0)
                    aMITesActivityCounters.Add(sensor_id, new MITesActivityCounter(this.mitesDecoders[0], sensor_id));
                    //aMITesActivityCounters.Add(sensor_id, new MITesActivityCounter(this.masterDecoder, sensor_id));
            }
            aMITesHRAnalyzer = new MITesHRAnalyzer(this.masterDecoder);//this.mitesDecoders[0]);
            aMITesDataFilterer = new MITesDataFilterer(this.masterDecoder);//this.mitesDecoders[0]);
            aMITesLoggerPLFormat = new MITesLoggerPLFormat(this.masterDecoder,//this.mitesDecoders[0],
                                                         dataDirectory + "\\data\\raw\\PLFormat\\");
            //aMITesLogger.SetActive(false);


            aMITesActivityLogger.WriteLogComment("Application started with command line: " +
                 dataDirectory + " " +
                 Constants.ACCEL_ID1 + " " +
                 Constants.ACCEL_ID2 + " " +
                 Constants.ACCEL_ID3 + " ");//+
            //Constants.ACCEL_ID4 + " " +
            //Constants.ACCEL_ID5 + " " +
            //Constants.ACCEL_ID6 + " ");

            // Set the correct channels based on sannotation automatically
            for (int i = 0; (i < this.sensors.TotalReceivers); i++)
            {
                int[] channels = new int[6];
                int channelCounter = 0;
                for (int j = 0; (j < this.sensors.Sensors.Count); j++)
                {
                    if (Convert.ToInt32(((Sensor)this.sensors.Sensors[j]).Receiver) == i)
                    {
                        int channelID=Convert.ToInt32(((Sensor)this.sensors.Sensors[j]).ID);
#if(PocketPC)
                        if (channelID != PhoneAccelerometers.Constants.BUILT_IN_ACCELEROMETER_CHANNEL_ID)
                        {
#endif
                            channels[channelCounter] = Convert.ToInt32(((Sensor)this.sensors.Sensors[j]).ID);
                            channelCounter++;
#if (PocketPC)
                        }
#endif
                    }
                }
                //Need to do the same thing for the Bluetooth
                if (this.configuration.Connection == MITesFeatures.core.conf.Constants.SOFTWARE_CONNECTION_USB)
                {
                    this.mitesControllers[i].SetChannels(this.sensors.GetNumberSensors(i), channels);
                }
            }

            return true;
        }
   //Initialize objects for logging and storing wockets and MITes data
   private void InitializeLogging(string dataDirectory)
   {
       aMITesActivityLogger = new MITesActivityLogger(dataDirectory + "\\data\\activity\\MITesActivityData");
       aMITesActivityLogger.SetupDirectories(dataDirectory);            
       aMITesLoggerPLFormat = new MITesLoggerPLFormat(this.masterDecoder,
                                                    dataDirectory + "\\data\\raw\\PLFormat\\");
     
       aMITesActivityLogger.WriteLogComment("Application started with command line: " +
            dataDirectory + " ");
       foreach (Receiver receiver in this.sensors.Receivers)            
           aMITesActivityLogger.WriteLogComment("Receiver"+receiver.ID+":"+receiver.Decoder+ " ");            
       foreach (SXML.Sensor sensor in this.sensors.Sensors)
           aMITesActivityLogger.WriteLogComment("Sensor" + sensor.ID + " ");            
 
   }