title: Reusing socket descriptor on connection failure
body: <p>In my client code, I am following these steps to connect to a socket:</p>
  
  <ol>
  <li><p>Creating a socket  </p>
  
  <pre><code>sockDesc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)  
  </code></pre></li>
  <li><p>Connecting it (retry for 'x' time in case of failure)  </p>
  
  <pre><code>connect(sockDesc, (sockaddr *) &amp;destAddr, sizeof(destAddr))  
  </code></pre>
  
  <p>(After filling the <code>destAddr</code> fields)  </p></li>
  <li><p>Using the socket for <code>send()</code>/<code>recv()</code> operation:  </p>
  
  <pre><code>send(sockDesc, buffer, bufferLen, 0)  
  recv(sockDesc, buffer, bufferLen, 0)  
  </code></pre></li>
  <li><p><code>close()</code> the socket descriptor and exit  </p>
  
  <pre><code>close(sockDesc)  
  </code></pre></li>
  </ol>
  
  <p>If during <code>send()</code>/<code>recv()</code> the connection breaks, I found that I could connect by returning to step 2.</p>
  
  <p>Is this solution okay? should I close the socket descriptor and return to step 1?</p>
  
  <p>Another interesting observation that I am not able to understand is when 
  I stop my echo server and start the client. I create a Socket (step 1) and call <code>connect()</code> which fails (as expected) but then I keep calling <code>connect()</code>, lets say, 10 times. After 5 retries I start the server and <code>connect()</code> is successful. But during the <code>send()</code> call it receives <code>SIGPIPE</code> error. I would like to know:</p>
  
  <p>1) Do I need to create a new socket every time <code>connect()</code> fails? As per my understanding as long as I have not performed any <code>send()</code>/<code>recv()</code> on the socket it is as good as new and I can reuse the same <code>fd</code> for the <code>connect()</code> call.  </p>
  
  <p>2) I don't understand why <code>SIGPIPE</code> is received when the server is up and <code>connect()</code> is successful.</p>