Ethernet sockets not closing on client.stop()

Hey all, I'm using the example WebServer code from the Ethernet library with a genuine Ethernet shield, and the web-server eventually stops responding. Sometimes it's a couple days, sometimes it's a couple of weeks. I ran into a post which had some code to display the state of the sockets, here (post #3):

So I added that, and am calling it in each loop. I've let it run for a couple of days now, and have one socket reporting 0x17. The example code calls client.stop(), so why is the socket still connected? The post referenced above also indicates this is a known problem, but doesn't hint at a solution. Is there one?

The shield is directly connected to a switch, and is not available to the internet. Thanks!

  Web Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.

 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi


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

#include <utility/w5100.h>
byte socketStat[MAX_SOCK_NUM];

// 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, 1, 1, 198);

// 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 showSockStatus()
 for (int i = 0; i < MAX_SOCK_NUM; i++) {
   uint8_t s = W5100.readSnSR(i);
   socketStat[i] = s;
   Serial.print(F(" "));
   Serial.print(F(" D:"));
   uint8_t dip[4];
   W5100.readSnDIPR(i, dip);
   for (int j=0; j<4; j++) {
     if (j<3) Serial.print(".");


void setup() {
  // Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

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

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 =;
        // 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: 5");  // refresh the page automatically every 5 sec
          client.println("<!DOCTYPE HTML>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(" is ");
        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
    // close the connection:
    Serial.println("client disconnected");

  // check sockets

  // small delay to not flood serial monitor

Sometimes it goes wrong and the socket stays open.
I did not have that problem myself, but to be sure I have added the checkSocketStatus() from here: Arduino Playground - WebServerST. That closes a unused socket that is still open.