checking a connection still exists?

Hi I have a bit of c code which I'm trying to use as a relay between apache and a scgi cluster.
Example of problem code is below:

 while\(\(n = recv\(scgiSock, local_data, MAX_LENGTH, 0\)\) > 0\) 
 \{
        time\(&t2\);
        time_now = t2 - t1;
    if\(time_now > TIMEOUT\)
	    throw 1;

#ifdef UN_NES_DEBUG
logger("scgi : length := ", 1, n);
#endif
logger("sending data: length = ", 1, n);
logger("send ret:= %d", 1, send(receiveSock, local_data, n, 0));
count++;
bzero(local_data, MAX_LENGTH + 1);
sum += n;
logger("Receiving data: current sum = ", 1, sum);
}

I'm encountering a slight but probably easily solveable problem; sometimes apache drops the connection and as a result my program crashes out when trying to send data back on the socket.
Is there any way to check that the socket is still alive before sending data?

Cheers.
Fish.

option 1. Use select() to tell you if there is anything to read and when you can write, if the connection drops then you will be told you can read something and will then get the appropriate error returned by "recv", only "send" if select tells you can write.

option 2. install a SIGPIPE signal handler around the send() to catch any raised signal, then restore the handler after the send.

thanks,
I'm actually using the select() function when I receive information from apache. but I didn't know you could use it to check for sending as well. I'll have to look into that a bit more. would you happend to have any examples of using select in a send situation?

I'm might as well set up the SIGPIPE signal error capture as well. you have any good examples of this?

Cheers.
Fish.