Most web servers have a 'time-out' parameter that limits how long the web server will try to produce a complete http response (send a page). If a web server does not see some/any output from your CGI over that 'time-out' period, then the web server will kill the process and time-out.
IF this is the problem, San suggested part of a solution, above. Set $| to 1. That sets the output buffer size to 0 causing anything that goes into the output buffer to be output immediately. AND, your CGI must produce some output on a frequency smaller than the time-out period. For instance, if your CGI makes a system call that takes a long time to complete, then you will need to figure out a way to keep shooting some output to the browser while the system call runs. The web server will see a trickle of output and will not time-out.
Another possible fix is to modify the configuration of the web server, set the time-out period to a longer value. However, this does not sound like an option in your situation. 'hope this helps
If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.