Ethernet shield locking up for lack of sockets

The Sketch refuses to be slimmed down, its at about 28000, so Ive looked for other ways of outputting what is going on by modifying your 'ShowSockStatus()' to output to a .CSV file on the SD card every loop! Ok so it slows things down but it shows the request to the Arduino for the web page only opens two sockets. I presuming that one is for the HTML and the other for the style sheet. So far so good. I've tried stress testing by firing up another PC to download the same page and the number of sockets in use only rises to 3. That still leaves some headroom for the occasional UDP request for an NTP time stamp from the web.

By the way the webpage being served by the Arduino has meta refresh call every 15 seconds so you can watch the changes in the system the Arduino is controlling.

Now if I include a 'checkSockStatus()',to kill off rogue external sniffing, the web page appears on the PC when requested for 30-40 seconds and then disappears! IE11 does not say why but Firefox gives the message 'Connection to server was reset whilst the page was loading'. The only way to get anything back is to reset the Arduino. So it would appear that the close(i) in the 'checkSockStatus()' is closing a socket that is actually in use! which is not good.

I've also tried an extra line in the 'checkSockStatus()' instead of the close(i); this returns the number of sockets open.

int CheckSockStatus()
{
 // Check for locked sockets every loop
int numOpen = 0; 
for (int i = 0;  i < MAX_SOCK_NUM; i++){
  uint8_t s = W5100.readSnSR(i);
  if((s == 0x17) || (s = 0x1C)) {
    //numOpen ++;
   if(systime - connectTime[i] > 60UL){
   //close(i);*/
     numOpen ++;
    }
  }
  else connectTime[i] = systime;
    //socketStat[i] = W5100.readSnSR(i);
  }
return numOpen;
}

Now when I add this to the record file that grows on the SD card it always shows '4' whereas the 'ShowSockStatus() output that the status of some of the sockets is 0x00.

I include an abstract copy of the record file below

Day/time Sockets in use Status
15 17:01:30 4 Socket#0 :0x14 80 D: 0.0.0.0 0
Socket#1 :0x0 8888 D: 192.168.1.254 -123
Socket#2 :0x0 0 D: 0.0.0.0 0
Socket#3 :0x0 0 D: 0.0.0.0 0
15 17:01:31 4 Socket#0 :0x14 80 D: 0.0.0.0 0
Socket#1 :0x0 8888 D: 192.168.1.254 -123
Socket#2 :0x0 0 D: 0.0.0.0 0 Getting initial time stamp
Socket#3 :0x0 0 D: 0.0.0.0 0
15 17:01:31 4 Socket#0 :0x14 80 D: 0.0.0.0 0
Socket#1 :0x0 8888 D: 192.168.1.254 -123
Socket#2 :0x0 0 D: 0.0.0.0 0
Socket#3 :0x0 0 D: 0.0.0.0 0
15 17:01:31 4 Socket#0 :0x14 80 D: 0.0.0.0 0

More of the same

15 17:01:40 4 Socket#0 :0x0 80 D: 192.168.1.66 -54757 Now serving web page?
Socket#1 :0x14 80 D: 192.168.1.254 -123 Still waiting for UDP?
Socket#2 :0x0 0 D: 0.0.0.0 0
Socket#3 :0x0 0 D: 0.0.0.0 0
15 17:01:40 4 Socket#0 :0x0 80 D: 192.168.1.66 -54757
Socket#1 :0x14 80 D: 192.168.1.254 -123
Socket#2 :0x0 0 D: 0.0.0.0 0
Socket#3 :0x0 0 D: 0.0.0.0 0
15 17:01:40 4 Socket#0 :0x0 80 D: 192.168.1.66 -54757
Socket#1 :0x14 80 D: 192.168.1.254 -123
Socket#2 :0x0 0 D: 0.0.0.0 0
Socket#3 :0x0 0 D: 0.0.0.0 0
15 17:01:40 4 Socket#0 :0x0 80 D: 192.168.1.66 -54757
Socket#1 :0x14 80 D: 192.168.1.254 -123
Socket#2 :0x0 0 D: 0.0.0.0 0
Socket#3 :0x0 0 D: 0.0.0.0 0
15 17:01:40 4 Socket#0 :0x0 80 D: 192.168.1.66 -54757
Socket#1 :0x17 80 D: 192.168.1.66 -54758
Socket#2 :0x0 0 D: 0.0.0.0 0
Socket#3 :0x0 0 D: 0.0.0.0 0
15 17:01:40 4 Socket#0 :0x14 80 D: 192.168.1.66 -54757
Socket#1 :0x17 80 D: 192.168.1.66 -54758
Socket#2 :0x0 0 D: 0.0.0.0 0
Socket#3 :0x0 0 D: 0.0.0.0 0

ETC ETC

I'm at a loss to know what is going on.