title: How to properly maintain a listening port for a long time?
body: <p>I wrote this small server application in pure C that listens to incoming connections in a given port, very simple stuff.</p>
  
  <p>It goes with the usual socket initialization procedure, create the <code>socket()</code> then <code>bind()</code> to the port, says its a <code>listen()</code>, and ifinitely loops through a <code>select()</code> waiting for incoming connections to <code>accept()</code>.</p>
  
  <p>All goes just fine and works like a charm, except that if I leave the thing running for a couple months, the listening port closes while the application server keeps running unaware of it, since I wrote it to trust the listening socket will not close if not told to.</p>
  
  <p>So the question is: Why the hell is the port being closed without my application's concern and what can I do to prevent it from happening?</p>
  
  <p>Is that expected behaviour? Should I check for some kind of exceptions or make "health check" on the listening socket to reopen it if necessary?</p>
  
  <p>Code: <a href="https://gist.github.com/Havenard/e930be035a3bee75c018" rel="nofollow noreferrer">https://gist.github.com/Havenard/e930be035a3bee75c018</a> (yes I realize I'm using <code>0</code> as cue for errors and it's bad pratice and stuff, but it is not relevant to the question as I explained in the comments, when I set the socket file descriptor to <code>0</code> it is to stop the loop and shut down the application).</p>