This is a version which doesn't always return the same connection if there are multiple open connections:
EthernetClient EthernetServer::available()
{
accept();
int rand = micros() % MAX_SOCK_NUM;
for (int s = 0; s < MAX_SOCK_NUM; s++) {
int sock = (s + rand) % MAX_SOCK_NUM;
EthernetClient client(sock);
if (EthernetClass::_server_port[sock] == _port &&
(client.status() == SnSR::ESTABLISHED ||
client.status() == SnSR::CLOSE_WAIT)) {
if (client.available()) {
return client;
}
}
}
return EthernetClient(MAX_SOCK_NUM);
}
EthernetClient EthernetServer::connected()
{
accept();
int rand = micros() % MAX_SOCK_NUM;
for (int s = 0; s < MAX_SOCK_NUM; s++) {
int sock = (s + rand) % MAX_SOCK_NUM;
EthernetClient client(sock);
if (EthernetClass::_server_port[sock] == _port &&
(client.status() == SnSR::ESTABLISHED ||
(client.status() == SnSR::CLOSE_WAIT && client.available()))) {
return client;
}
}
return EthernetClient(MAX_SOCK_NUM);
}
It's not completely random but it's better than using always the same order to look for the next socket to handle.