title: Determine between socket and fd
body: <p>On unix <strong><em>everything is a file</em></strong> approach of function <code>read()</code>, <code>write()</code>, <code>close()</code> is not supported on Win32.</p>
  
  <p>I want to emulate it but have no idea how to distinguish when <code>sock</code> is <strong>socket</strong> or <strong>fd</strong> on WinSocks2.</p>
  
  <pre><code>//returns 1 if `sock` is network socket, 
  //        0 if `sock` is file desriptor (including stdio, stderr, stdout), ...
  //       -1 in none of above
  int is_net_socket(int sock)
  {
      // ...?
  }
  </code></pre>
  
  <p>This should work as in :</p>
  
  <pre><code>int mysock  = socket(PF_INET, SOCK_STREAM, 0);
  int myfd    = _open("my_file.txt", _O_RDONLY);
  
  printf("1: %d    2: %d    3: %d    4:%d\n",
         is_net_socket(mysock),   //1
         is_net_socket(myfd),     //0
         is_net_socket(stdin),    //0
         is_net_socket(stderr));  //0
  
  // should print "1: 1    2: 0    3: 0    4:0"
  </code></pre>
  
  <hr>
  
  <p>How to implement <code>is_net_socket</code> in order to use it as in:</p>
  
  <pre><code>int my_close(int sock)
  {
  #if ON_WINDOWS
      switch( is_net_socket(sock) ) {
          case 1: return closesocket(sock);
          case 0: return _close(sock);
          default: //handle error...
      }
  #else
      return close(sock);
  #endif
  }
  </code></pre>