And, actually due to the optimizer, you may also have to do this in a while loop so that the array pointer doesn't get optimized down.[pascal]procedure TXServer.Finalize;
var
socket: PTCPSocket;
begin
SDLNet_UDP_Close(fUDPSocket);
while fAllSockets.numsockets>0 do
begin
socket := PXTCPSocketArray(fAllSockets.sockets)[fAllSockets.numsockets-1];
SDLNet_TCP_Close(socket);
end;
SDLNet_TCP_DelSocket(fAllSockets, fTCPSocket);
SDLNet_FreeSocketSet(fAllSockets);
fConnected := False;
end;[/pascal]

This is what I had to do in my TCP wrapper for SDL, its on the board some place if you want a reference. Something about some versions of FPC and/or Delphi optimizing down the array pointer for speed improvements. At times, SDL will move pointers in the background for you. As William said though, you do have to go from the top of the list to the bottom