Help with WebSockets using Arduino Mega and USR-WIFI232

I have not seen code on the web that works with the Arduino Mega and the USR-WIFI232 so I set out to develop my own and post it. After several failed attempts I decided hire a developer to help and this is as far as he is willing to go.

I would like some help trouble shooting this code. I am attempting to connect the board to a websocket server. I have successfully connected to websocket.org. However, it takes several attempts (20+ times through the while loop). Also, the main goal is to connect to a socket.io server which I have not successfully accomplished.

The two functions which are used for establishing a connection are “Connect()” and “ConnectToWebSckServer()”. The rest of the code (see attachment) preceding these functions deals with initializing the WIFI module, this part works, but I am sure can be improved.

I am looking for suggestions in improving the board connectivity or at least an explanation on why it fails to connect after several attempts. And also, how to properly send the handshake and connect to a socket.io server.

==============================================================================
//Connect() function will send commands to WiFi module to Connect to WebSocke
//Server.
//==============================================================================
void Connect()
{
  String Response = String();
  Serial.print("Connecting to Web Socket Server......");

  Serial1.print("AT+TCPDIS=on\r");   //Turn On TCP Link
  DelayinMS(200);
  while(Serial1.available())
  {
    Response += (char )Serial1.read();
    DelayinMS(20);
  }
  //while(ConnectToWebSckServer("/socket.io/1/","52.0.167.93:3000","http://52.0.167.93:3000"));
  while(ConnectToWebSckServer("/","echo.websocket.org","http://echo.websocket.org"));

  digitalWrite(CONNECTED, HIGH);
  Serial.println("..... Connected.");
}
//------------------------------------------------------------------------------

//==============================================================================
//ConnectToWebSckServer() function
//==============================================================================
int ConnectToWebSckServer(String Path, String Host, String Origin)
{
   String HandShake = String();
   String Comm = String();
   String Response = String();  
   //Prepare header for connecting to WebSocekt Server
   HandShake = "GET ";
   HandShake += Path;
   HandShake += " HTTP/1.1\r\n";
   HandShake +="Upgrade: WebSocket\r\n";
   HandShake+="Connection: Upgrade\r\n";
   HandShake +="Host: ";
   HandShake += Host;
   HandShake +="\r\nOrigin: ";
   HandShake += Origin;
   HandShake += "\r\n\r\n";
   //Serial.println(HandShake);
   
   do{
     Response = String();
     Comm = String("AT+SEND=") + String(HandShake.length()) + String("\r");
     while(!SendComm(Comm, ">")) DelayinMS(1000);
     DelayinMS(300);
     Serial1.print(HandShake);
 Serial.println(HandShake);

     DelayinMS(3000);
     while(Serial1.available())
     {
       Response += (char )Serial1.read();
       DelayinMS(20);
     } 
 

     Response = Response.substring(4, (Response.length()-4));
 Serial.println(Response);
     }while(!Response.equals("120"));// 135 for socket.io??
   //Receive response from the server
   Serial1.print("AT+RECV=1000\r");
   DelayinMS(5000);
   Response = String();
   while(Serial1.available())
   {
     Response += (char )Serial1.read();
     DelayinMS(20);
   }
   //Check if the response has proper data
   if(Response.indexOf("HTTP/1.1 101") >= 0) return 0;
   else return 1;
}
//------------------------------------------------------------------------------

//==============================================================================

//==============================================================================
//SendComm(ATComm, ExpRes) function accepts two strings as parameter. ATComm is
//the command that is to be sent to WiFi module and ExpRes is the expected
//response from the WiFi module. The function returns 1 if the response from the
//module is as per expected.
//Value Returned : Integer    1: Command Executed Successfully.
//                            0: Command not Executed Successfully.
//==============================================================================
int SendComm(String ATComm, String ExpRes)
{
  String Response = String();
  Serial1.print(ATComm);
  DelayinMS(200);
  while(Serial1.available())
  {
    Response += (char )Serial1.read();
    DelayinMS(20);
  }
  if(!(Response.indexOf(ExpRes) >= 0)) return 0;    //Check if Module's response
                                                    //is as expected.
  //Response = Response.substring(ATComm.length(), (Response.length()-4));
  //if(Response != ExpRes) return 0;
  return 1;
}
//------------------------------------------------------------------------------

//SendData()Function will send the string data received in the parameter "Data"
//to the WebSocket Server
//Value Returned : Integer    1: If data sent successfully.
//                            0: If any error in Data Sending
//==============================================================================
int SendData(String Data)
{
  String Response = String();
  String Comm = String();
  int i;
  char ch;
  
  Comm = String("AT+SEND=") + String(Data.length()+2) + String("\r");
  while(!SendComm(Comm, ">")) DelayinMS(50);
  DelayinMS(200);
  ch=0x00;
  //Comm = String(ch) + Data;
  Serial1.write(ch);
  for(i=0; i < Data.length(); i++) Serial1.write(Data.charAt(i));
  ch=0xFF;
  //Comm = Comm + String(ch);
  Serial1.write(ch);
  //Serial1.print(Comm);
  DelayinMS(2000);
  while(Serial1.available())
  {
    Response += (char )Serial1.read();
    DelayinMS(20);
  }
  Response = Response.substring(4, (Response.length()-4));
  if(Response.equals(String(Data.length()+2))) {
    Serial.print("Data Sent - ");
    Serial.println(Data);
    return 1;
  }
  Serial.print("Error in Sending Data - ");
  Serial.println(Data);
  return 0;
}
//------------------------------------------------------------------------------


//==============================================================================
//ReceiveData() Function will receive data (maximum 1000 bytes at a time)
//Return Value : String Received from WebSocket Server.
//==============================================================================
String ReceiveData()
{
  String Response = String(); 
  Serial1.print("AT+RECV=1000\r");
  DelayinMS(5000);
  while(Serial1.available())
  {
    //Serial.write(Serial1.read());
    Response += (char )Serial1.read();
  }
  Response = Response.substring(22, (Response.length()-5));
  Serial.print("Data Recd - ");  
  return Response;
}
//------------------------------------------------------------------------------

Is it possible for one to determine to correct header for the arduino to send from this screen shot? I am attempting to connect to the same server.

Any thoughts on this??