Wifi led controlling program mistake

Hello,

Yesterday i made (compiled) a sketch to turn an LED on and of at a webpage in a browser.
Everything looks nice, until you try to turn the led off and back on, sometimes it works, sometimes not. (see serialmonitor.jpg)
What is the fault/mistake in my program?

/*
  WiFi Web Server

 A simple web server that shows the value of the analog input pins.
 using a WiFi shield.

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the Wifi.begin() call accordingly.

 Circuit:
 * WiFi shield attached
 * Analog inputs attached to pins A0 through A5 (optional)

 created 13 July 2010
 by dlf (Metodo2 srl)
 modified 31 May 2012
 by Tom Igoe

 */

#include <SPI.h>
#include <WiFi.h>


char ssid[] = "telenet-apn-3DBF9";      // your network SSID (name)
char pass[] = "---***---";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
String readString;


int status = WL_IDLE_STATUS;

WiFiServer server(80);

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
pinMode(4, OUTPUT);
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if ( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();
}


void loop() {
  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
//    boolean currentLineIsBlank = true;
    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.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' ) {
          
//          && currentLineIsBlank
       
           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("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>ROBOPROTECT Arduino button</H1>");
          
          // For simple testing, pin 5, 6, 7, and 8 are used in buttons
          // DIY buttons
          
            

          client.println("<input type=button value=ON onmousedown=location.href='/?on4;'>"); 
          client.println("<input type=button value=OFF onmousedown=location.href='/?off5;'>");
          client.println("</BODY>");
          client.println("</HTML>");
        
                          delay(100);
           //stopping client
           client.stop();


           
           if(readString.indexOf('4') >0)//checks for 4
          {
            digitalWrite(4, HIGH);    // set pin 6 high
            Serial.println("Led 4 On");
          }
          if(readString.indexOf('5') >0)//checks for 5
          {
            digitalWrite(4, LOW);    // set pin 6 low
            Serial.println("Led 4 Off");
          }
          
                       
          //clearing string for next read
          readString="";
          

         
          
          
          

      }
      }
    }

  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Take a look at this thread: http://forum.arduino.cc/index.php?topic=267339.msg1886703 If you have questions after reading that, post them here.

FYI, the wifi shield server library is still a bit unstable.

I have read the posts in the link and edited my code.
But it still not works perfectly.

/*
  WiFi Web Server

 A simple web server that shows the value of the analog input pins.
 using a WiFi shield.

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the Wifi.begin() call accordingly.

 Circuit:
 * WiFi shield attached
 * Analog inputs attached to pins A0 through A5 (optional)

 created 13 July 2010
 by dlf (Metodo2 srl)
 modified 31 May 2012
 by Tom Igoe

 */

#include <SPI.h>
#include <WiFi.h>


char ssid[] = "telenet-apn-3DBF9";      // your network SSID (name)
char pass[] = "-------";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
String readString;


int status = WL_IDLE_STATUS;

WiFiServer server(80);

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
pinMode(4, OUTPUT);
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if ( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();
}


void loop() {
  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
//    boolean currentLineIsBlank = true;
    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.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' ) {
          
//          && currentLineIsBlank
       
           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("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>ROBOPROTECT Arduino button</H1>");
          
          // For simple testing, pin 5, 6, 7, and 8 are used in buttons
          // DIY buttons
          
            

//          client.println("<input type=button value=ON onmousedown=location.href='/?on4;'>"); 
//          client.println("<input type=button value=OFF onmousedown=location.href='/?off5;'>");
          client.print("<input type=submit value=ON style=width:100px;height:45px onClick=location.href='/?on4'>");
          client.print("<input type=submit value=OFF style=width:100px;height:45px onClick=location.href='/?off5'>");
          client.println("</BODY>");
          client.println("</HTML>");
        
                          delay(100);
           //stopping client
           client.stop();


           
           if(readString.indexOf('on4') >0)//checks for 4
          {
            digitalWrite(4, HIGH);    // set pin 6 high
            Serial.println("Led 4 On");
          }
          if(readString.indexOf('off5') >0)//checks for 5
          {
            digitalWrite(4, LOW);    // set pin 6 low
            Serial.println("Led 4 Off");
          }
          
                       
          //clearing string for next read
          readString="";
          

         
          
          
          

      }
      }
    }

  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Whats the problem?

But it still not works perfectly.

It does something. You expect it to do something.

Whats the problem?

Isn't it obvious? Those two somethings aren't the same thing.

Of course, what either of them is is a mystery.

When i load the program in to the arduino. Then open the serial monitor, wait until the connection is ok. Then i open my browser (Firefox) en go to the page (IP adres). There i can see the 2 buttons (on and off). When i press On, the led lights up, when i press off, the LED stops. When i press a second time on, the LED dos not work, en when i do this a few time's the LED lights up. (I just looks like its random). The same problem for the off button. And sometimes when i press the off button, the LED lights up. For me, its kind of a mystery.

For me, its kind of a mystery.

    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

Why not print what the client is asking for, and resolve the mystery?

If there is a breakdown in communications, that is one thing. If the client is asking for something, and you respond inappropriately, that is another thing.

So what i have to do? Because Serial.println(client.read()); just gives a ton of numbers.

The wifi shield server library is a bit unstable. This should display the request.

    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(ch);