Ethernet shield control through local wifi and internet (twitter)

I’m trying to achieve two things and am having difficulty pulling it off. I have an Uno with an Ethernet shield which is connected to an RF remote that controls lights. I am able to instantly turn on and off my lights using any device connected to my wi-fi. What I’m looking to do is to also have Twitter support. So if I tweet anything that starts with an “h”, I want it to turn my lights on. I have some code that is able to achieve this, except there’s a few problems that I’m struggling with.

For some reason, when I run my program, both the wifi and twitter functions stop working. If I comment out one out of the two, it works. To make the checkTwitter function work, I have to comment out the entire checkWifi function, not only the one in the main loop, therefore I think the problem is coming from my checkWifi function. I thought it might be the client variable, so I made two separate ones, but no luck. I believe it may have something to do with the server port, but I’m not sure how to fix it. Any help will be appreciated.

#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 
  192, 168, 1, 127 }; // ip in lan
byte gateway[] = { 
  192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
EthernetClient client;
EthernetClient client2;
String readString; 
String TwitterName ="josemunoz552"; //change this to your own twittername, or follow me ;-)
char tweet[140];
byte charsize2;
char serverName[] = "api.twitter.com";
String SearchDate="<description>josemunoz552: ";
char code[] = "h";
char Twitdate[140];
long TwitterCheckInterval=60000;
unsigned long lastTwitterCheck;
//////////////////////

void setup(){

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  //the pin for the servo co
  //enable serial data print 
  Serial.begin(9600);
}

void loop(){
  // Create a client connection
  checkWifi();
  if (millis()-lastTwitterCheck > TwitterCheckInterval){
        checkTwitter();
    Serial.println("checking Twitter now");
    lastTwitterCheck += TwitterCheckInterval;

  }
}

void checkWifi(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

            client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<style> body { background-color:#d0e4fe; }</style>");

          client.println("<TITLE>Moe's Home Automation</TITLE>");

          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Moe's Home Automation</H1>");

          client.println("<hr />");

          client.println("
");

          client.println("<a href=\"/?lighton1\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>"); 
          client.println("<a href=\"/?lightoff1\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>

");
          client.println("<a href=\"/?lighton2\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>"); 
          client.println("<a href=\"/?lightoff2\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>

");  
          client.println("<a href=\"/?lighton3\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>"); 
          client.println("<a href=\"/?lightoff3\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>

");       

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("?lighton1") >0)//checks for on
          {
            digitalWrite(2, HIGH);    // set pin 2 high
            digitalWrite(8, HIGH);
            delay(250);
            digitalWrite(2, LOW);    // set pin 2 high
            digitalWrite(8, LOW);
            Serial.println("Led 1 On");
            client.println("<a href=\"/?lightAon\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>");
          }
          else{
            if(readString.indexOf("?lightoff1") >0)//checks for off
            {
              digitalWrite(4, HIGH);    // set pin 2 high
              digitalWrite(8, HIGH);
              delay(250);
              digitalWrite(4, LOW);    // set pin 2 high
              digitalWrite(8, LOW);    // set pin 4 low
              Serial.println("Led 1 Off");
              client.println("<link rel='apple-touch-icon' href='http://files6.fliiby.com/images/_original/ud5qurg7qw.png' />");
            }
          }
          //clearing string for next read

          if(readString.indexOf("?lighton2") >0)//checks for on
          {
            digitalWrite(3, HIGH);    // set pin 2 high
            digitalWrite(8, HIGH);
            delay(250);
            digitalWrite(3, LOW);    // set pin 2 high
            digitalWrite(8, LOW);    // set pin 4 low
            Serial.println("Led 2 On");
            client.println("<link rel='apple-touch-icon' href='http://files6.fliiby.com/images/_original/ud5qurg7qw.png' />");
          }
          else{
            if(readString.indexOf("?lightoff2") >0)//checks for off
            {
              digitalWrite(5, HIGH);    // set pin 2 high
              digitalWrite(8, HIGH);
              delay(250);
              digitalWrite(5, LOW);    // set pin 2 high
              digitalWrite(8, LOW);    // set pin 4 low
              Serial.println("Led 2 Off");
              client.println("<link rel='apple-touch-icon' href='http://files6.fliiby.com/images/_original/ud5qurg7qw.png' />");
            }
          }
          //clearing string for next read

          if(readString.indexOf("?lighton3") >0)//checks for on
          {
            digitalWrite(6, HIGH);    // set pin 2 high
            digitalWrite(8, HIGH);
            delay(250);
            digitalWrite(6, LOW);    // set pin 2 high
            digitalWrite(8, LOW);    // set pin 4 low   // set pin 2 high
            Serial.println("Led 3 On");
            client.println("<link rel='apple-touch-icon' href='http://files6.fliiby.com/images/_original/ud5qurg7qw.png' />");
          }
          else{
            if(readString.indexOf("?lightoff3") >0)//checks for off
            {
              digitalWrite(7, HIGH);    // set pin 2 high
              digitalWrite(9, HIGH);    // YO
              digitalWrite(8, HIGH);
              delay(250);
              digitalWrite(7, LOW);    // set pin 2 high
              digitalWrite(9, LOW);    // YO
              digitalWrite(8, LOW);    // set pin 4 low;    // set pin 4 low
              Serial.println("Led 3 Off");
              client.println("<link rel='apple-touch-icon' href='http://files6.fliiby.com/images/_original/ud5qurg7qw.png' />");
            }
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}
void checkTwitter(){
  if (client2.connect(serverName, 80)) {
    TextFinder  finder( client2,2 );
    //    Serial.println("HTTP Request Initialized");
    // make HTTP GET request to twitter:
    client2.print("GET /1/statuses/user_timeline.rss?screen_name=");
    client2.print(TwitterName);
    client2.println("&count=1 HTTP/1.1");
    client2.println("HOST: api.twitter.com");
    client2.println();
    while (client2.connected()) {
      if (client2.available()) {

        Serial.println("Searching for new tweet");
        charsize2 = SearchDate.length();
        char StartHere[charsize2];  
        char EndHere[] = "</title>";
        char EndDate[]= "</pubDate>"; 
        SearchDate.toCharArray(StartHere,charsize2);
        if((finder.find("<item>")&&(finder.getString(StartHere,EndHere,Twitdate,140)!=0)))
          Serial.println(Twitdate);
        Serial.println(Twitdate[1]);
        if(Twitdate[1]==code[0]){
          digitalWrite(9, HIGH);
        }
        else{
          digitalWrite(9, LOW);
        }


        break;
      }
    }
    delay(1);
    client2.stop();
  }
}

One thing might be you are running out of memory. Use the F() macro for your static text like below.

client.print(F("<input type='button' value='Invia' onclick='window.open(\"/?tmin=\" + document.getElementById(\"tmin\").value"));
          client.println(F(",\"_blank\",\"toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=yes, width=400, height=400, top=100, left=500\")'>"));
          client.println(F("<input type=text id='tmin' SIZE=3>"));
          client.print(F("
SetPoint Spegnimento.: "));      
          client.print(mydata.tSSRMaxES);   // temperatura per spegnimento relé
          client.print(F(" C --- "));      
          client.print(F("<input type='button' value='Invia' onclick='window.open(\"/?tmax=\" + document.getElementById(\"tmax\").value"));
          client.println(F(",\"_blank\",\"toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=yes, width=400, height=400, top=100, left=500\")'>"));
          client.println(F("<input type=text id='tmax' SIZE=3>"));