Idea: Start a sub-process (other script?) for every connection, like a proxy. Every connection process lifes as long as it's connection and the main process could use a local fifo|socket|etc. to talk to the connection process.

The main script may run forever without eating up more memory while the connection processes leak memory (1 leak per connection = 1 leak per proccess) and free the memory once the connection is finished.