title: C# Modbus/tcp - hanging connection
body: <p>I have written Windows service, which perform Modbus WriteMultipleRegisters function  call over TCP using <a href="https://code.google.com/p/nmodbus/" rel="nofollow">NModbus</a> library to 3-party devices every 10 minutes (ticks of System.Threading.Timer).</p>
  
  <p>Occasionally this connection hang up open usually during network problems. As the device accepts only one Modbus connection at time and others are refused, connection during all next ticks fail with SocketException - ConnectionRefused. </p>
  
  <p>But the device automatically closes connections which don't respond after short time. Something must keep connection open at my side even for two days. What's more when my Service is restarted, everything is fine again. So there is definitely some forgotten open connection. But I didn't manage to reproduce this bug in dev, so I don't where/when.. connection hang up. I only know that next connection is refused.</p>
  
  <p>I do the modbus function call with this part of code:</p>
  
  <pre><code>using (TcpClient client = new TcpClient(device.ip, 502))
      {
      using (Modbus.Device.ModbusIpMaster master = Modbus.Device.ModbusIpMaster.CreateIp(client))
            {
            master.WriteMultipleRegisters(500, new ushort[] { 0xFF80 });
      }  
  }
  </code></pre>
  
  <p>device.ip is string containing IP address of device - it's correct, confirmed from SocketException details.</p>
  
  <p>As I'm using using statement dispose is called on both objects.
  I have looked trough NModbus source code and everything is disposed correctly.</p>
  
  <p>Any idea how its possible that with this code connection is not closed?</p>