 
   if (prot == TCP){
     if(!SendCIPSTART(TCP)){
-      Serial.println("AT+CIPSTART => FAIL"); 
-    } else Serial.println("AT+CIPSTART => OK");
+      delay(1);
+      //Serial.println("AT+CIPSTART => FAIL"); 
+    } else {
+      delay(1);
+      //Serial.println("AT+CIPSTART => OK");
+    }
   }else {
     if(!SendCIPSTART(UDP)){
-      Serial.println("AT+CIPSTART => FAIL"); 
-    } else Serial.println("AT+CIPSTART => OK");    
-  }
-  
-  delay(1000);
+      delay(1);
+      //Serial.println("AT+CIPSTART => FAIL"); 
+    } else {
+      delay(1);
+      //Serial.println("AT+CIPSTART => OK");    
+    }
+  }
+  
+  //delay(1000);
+  delay(100);
 
   //copy latitude and longitude to send via TCP server
   memset(data2db,'\0',sizeof(data2db));

   if(token != NULL) {
      strncpy(east_west,token,sizeof(east_west));
      east_west[sizeof(east_west)-1] = '\0';      
      Serial.print("E/W INDICATOR   : ");
      Serial.println(east_west);    
      //Serial.println("************************************************************ ");  
        
   }else return false;

   if(Serial.available())
      GPRS.write(Serial.read());
  #endif
  

  //Handling of make and receive call
  if(sim808.readable()){
    sim808_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
    Serial.print(gprsBuffer);
  
    //**** Detect the current state of the telephone or SMS ****************
    if(NULL != strstr(gprsBuffer,"RING")) {
        sim808.answer(); 
        if(extractCallingNumber(gprsBuffer)){
          sim808.sendSMS(phone,smsMessage);
        }else{
          Serial.println("No CLIP to send SMS");
        }
        
        //sim808_send_cmd("AT+CTTS=2,\"hello,欢迎使用语音合系统\"\r\n");  
        //sim808.sendSMS(PHONE_NUMBER,smsMessage);
    
    }else{
      if(NULL != (s = strstr(gprsBuffer,"+CMTI: \"SM\""))) { //SMS: $$+CMTI: "SM",24$$
        sim808_clean_buffer(gprsBuffer,32); 

        if(!sim808_check_with_cmd("AT+CMGF=1\r\n","OK\r\n",CMD)){ //Set SMS for text mode not HEX when read it
          Serial.println("fail CMGF");     
        }else {
          Serial.println("ok CMGF");
        }
        delay(25);
   
        GPRS.println("AT+CMGR=1");
        sim808_read_buffer(gprsBuffer,64,DEFAULT_TIMEOUT);
        Serial.print(gprsBuffer);

        if(!sim808_check_with_cmd("AT+CMGDA=\"DEL ALL\"\r\n","OK\r\n",CMD)){ //delete all SMS
          Serial.println("fail del SMS");     
        }else {
          Serial.println("ok del SMS");
        }

        
        if(extractCallingNumberSMS(gprsBuffer)){
          sim808.sendSMS(phone,smsMessage);
        }else{
          Serial.println("No valid # for SMS");
        }
      }
     
    }
    sim808_clean_buffer(gprsBuffer,32);       
  }  


}

/****************************************************************************************
* extractCallingNumber(char *messages)
* This function when an incoming call come and CLIP is enabled, it will prompt at serial
* the message:
*   RING
*   
*   +CLIP: "18968192062",161,"",0,"",0
* 
* Here we extract the phone number
*****************************************************************************************/
boolean extractCallingNumber(char *inMessage){
char *token;

   /* get the first token */
   //Serial.println("valor do buffer :");
   Serial.println(inMessage);

   if(NULL == strstr(inMessage,"+CLIP")){
     return false;
   }
   
   //Get Command sent
   token = strtok(inMessage,"\n\r" );
   if(token == NULL)  return false;

   //Get "+CLIP: " string
   token = strtok(NULL, "\"");   
   if(token == NULL)  return false;
   
   //Get phone number value
   token = strtok(NULL, "\"");
   if(token != NULL) {    
      strncpy(phone,token,sizeof(phone));
      phone[sizeof(phone)-1] = '\0';

      //strcpy(gpsMode,token);
      Serial.print("phone    : ");
      Serial.println(phone);   
   }else return false;
   
   return true;
  
}

/****************************************************************************************
* extractCallingNumberSMS(char *messages)
* This function when receive an SMS extract the calling number from SMS.
* AT+CMGR=#           //read the message # 
* 
* +CMGR: "REC READ","18968192062","","17/10/14,14:07:10+32"* 
* 
* Here we extract the phone number
*****************************************************************************************/
boolean extractCallingNumberSMS(char *inMessage){
char *token;
char temp[16];



   /* get the first token */
   //Serial.println("valor do buffer :");
   Serial.println(inMessage);

   if(NULL == strstr(inMessage,"+CMGR")){
     return false;
   }
   
   //Get Command sent
   token = strtok(inMessage,"\n\r" );
   if(token == NULL)  return false;

   //Get "+CMGR:" string
   token = strtok(NULL, ":");   
   if(token == NULL)  return false;

   //Get type of SMS "REC READ" or other type not important here 
   token = strtok(NULL, ",");   
   if(token == NULL)  return false;


   
   //Get phone number value
   token = strtok(NULL, ",");
   if(token != NULL) {    
      //removing the " at begining and end of phone number
      //token = phone;
      token++;
      token[strlen(token)-1]='\0'; //now the " is not at beginning/end of toke and in phone either
      
      strncpy(phone,token,sizeof(phone));
      phone[sizeof(phone)-1] = '\0';

      //getAllButFirstAndLast(token,phone);

      
      //strcpy(gpsMode,token);
      Serial.print("phone: ");
      Serial.println(phone);   
   }else return false;
   
   return true;
 
}

/*************************************************************************************************
*  getAllButFirstAndLast(const char *input, char *output)
*  Ths procedure remove the first and last character of char array 
*  "12938484848"
*  for exemplo for a phone number
************************************************************************************************/
void getAllButFirstAndLast(const char *input, char *output)
{
  int len = strlen(input);
  if(len > 0)
    strcpy(output, ++input);
  if(len > 1)
    output[len - 2] = '\0';
}


