Web Server works great! But when I try to reconnect again later, can't find it..

I basically would like to run a simple web server using the sample code given. But instead of just simply outputting the Analog input values to a web browser, I'd like to output a pH value. I am taking 1000 values with 2 ms inbetween each, then reordering them from least to greatest and only averaging the 6 medians. I find this is necessary to get a consistent reading from RobotMesh's pH sensor. Is this an overuse of SRAM?

So here's my problem:

I can upload to my MEGA 2560 and I can see the output value on my browser at the given IP (work as it should).
Then I close my browser or leave that page, or any other action that will disconnect the server.
Now I cannot find the web server at the given IP (or any other IP of course).
I can upload to my MEGA 2560 with a different IP address and the problem is solved... until I disconnect again. (rinse and repeat)

Below is my code... Any suggestions are welcome... Also I am not very knowledgeable on networking, but I can get by as a Controls Engineer... I am also very new at programming web servers/clients/ anything through networking... etc.

Also if there is any fat that should be trimmed in my program let me know, like I said I took most of it form the example and didn't take time to go line by line to get an understanding for it yet... (since it doesnt work properly, I'm doing that now, but thought some input from people on my particular problem may be useful)

Thanks in advance!

/*
Web Server

A simple web server that shows the value of the pH Sensor.
using an Seeeduino Ethernet shield.

Circuit:

  • Ethernet shield attached to pins 2, 3, 10, 11, 12, 13

modified 24 Feb 2014
by Nick Lemmer

*/

#include <SPI.h>
#include <Ethernet.h>
#define pHSensorPin 0 //pH meter Analog output to Arduino Analog Input 0
#define Offset 0.00 //deviation compensate
unsigned long int avgValue; //Store the average value of the sensor feedback

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,0,0,21);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.println("Ready"); //Test the serial monitor
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop() {
// listen for incoming clients
EthernetClient 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();
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) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 2"); // refresh the page automatically every 2 sec
client.println();
client.println("");
client.println("");

// take reading from pH Sensor
int buf[10]; //buffer for read analog
for(int i=0;i<10;i++) //Get 1000 sample value from the sensor for smooth the value
{
buf*=analogRead(pHSensorPin);*

  • delay(2);*
  • }*
  • for(int i=0;i<999;i++) //sort the analog from small to large*
  • {*
  • for(int j=i+1;j<1000;j++)*
  • {*
    _ if(buf*>buf[j])_
    _
    {_
    _ int temp=buf;
    buf=buf[j];
    buf[j]=temp;
    }
    }
    }
    avgValue=0;
    for(int i=497;i<503;i++) //take the average value of 6 center sample*

    avgValue+=buf*;
    float phValue=(float)avgValue5.0/1024/6; //convert the analog into millivolt_

    _ phValue=3.5phValue+Offset; //convert the millivolt into pH value_
    _ Serial.print(" pH:");
    Serial.print(phValue,2);
    Serial.println(" ");
    // output the value of the pH Sensor from Robot Mesh*

    * client.println("
    ");
    client.print(" pH:");
    client.print(phValue,2);
    client.println(" ");
    client.println("
    ");
    // output the value of each analog input pin*

    * for (int analogChannel = 1; analogChannel < 6; analogChannel++) {
    int sensorReading = analogRead(analogChannel);
    client.println("
    ");
    client.print("analog input ");
    client.print(analogChannel);
    client.print(" is ");
    client.print(sensorReading);
    client.println("
    ");
    }
    client.println("");
    break;
    }
    if (c == '\n') {
    // you're starting a new line*

    * currentLineIsBlank = true;
    }
    else if (c != '\r') {
    // you've gotten a character on the current line*

    * currentLineIsBlank = false;
    }
    }
    }
    // give the web browser time to receive the data*

    * delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
    }
    }*_

#7 below would help. In your browser do you use the http URL format like http://10.0.0.21 in the browser URL box?

http://forum.arduino.cc/index.php/topic,148850.0.html