title: python socket, invalid argument
body: <p>I'm working with an <a href="https://www.controlanything.com/Relay/Device/R130PL_ETHERNET" rel="nofollow noreferrer">ethernet enabled relay</a>. Trying to communicate with it with python over a socket. I'm seeing very interesting behavior and I don't think it is device related. Below is the code that fails. The error I'm getting is <code>[Errno 22] Invalid argument</code></p>
  
  <pre><code>    self.ip_addr = &lt;some address&gt;
      self.port = &lt;some port&gt;
      self.max_attempt_cnt = &lt;some count&gt;
      self.sleep_interval = &lt;some interval&gt;
      try_cnt=0
      skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)        
      while True:
          try:
              skt.connect((self.ip_addr, int(self.port)))
              break
          except socket.error as e:
              try_cnt+=1
              if (self.max_attempt_cnt &gt; 0) and (try_cnt &gt; self.max_attempt_cnt):
                  msg = 'Maximum connection attempt count of {} is exceeded'.format(
                          self.max_attempt_cnt
                          )
                  raise ConnectionAttemptCountExceededError(msg)
              time.sleep(self.sleep_interval)
  
      &lt;do stuff 0&gt;
      &lt;do stuff 1&gt;
      &lt;do stuff 2&gt;
  </code></pre>
  
  <p>If I move my defition of <code>skt</code> inside the <code>while</code> loop, i.e like the following:</p>
  
  <pre><code>    while True:
          try:
              skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
              skt.connect((self.ip_addr, int(self.port)))
              break
  </code></pre>
  
  <p>The issue disappears.</p>
  
  <p>I know there is a different way to write this code but that is not what I'm asking. </p>
  
  <p>My question is why the code is failing when the socket is created outside the loop and why it works when the socket is created inside the loop?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I realized that I didn't mention that this code does work on <code>Windows</code>, but it doesn't work on a <code>Mac</code> machine, which is what I'm using.</p>