Try using this:
set_time_limit(0);
this will however cause the script to NEVER stop executing until it ends. (Meaning if you execute it via a webserver, it'll keep that process running...). The best way to have something stay connected to something is to just run it as a seperate process using the php cgi executable like so:
php -q /path/to/script.php
Also, using pfsockopen will automatically reconnect to the current open persistent socket. You do not need to do anything special.