using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace Tor.Extensions
{
public static class Extensions
{
///
/// Determine if socket is connected
/// "Poll" returns true if
/// connection is closed, reset, terminated or pending (meaning no active connection)
/// connection is active and there is data available for reading
/// "Available" returns number of bytes available for reading
/// if both are true:
/// There is no data available to read so connection is not active
///
/// The socket
public static bool IsConnected(this Socket socket)
{
try
{
bool part1 = socket.Poll(1000, SelectMode.SelectRead);
bool part2 = (0 == socket.Available);
return part1 && part2 ? false : true;
}
catch(Exception)
{
return false;
}
}
}
}