"Always stay connected" mechanism for socket connection I'm calling Connect() for the first time I connect, I am then checking if the connection is still connected every 10 seconds if it is not connected it attempts to re-connect.

public class ClientHandler : ISocketHandler, IDisposable
{
    private readonly Socket _socket;
    private readonly byte[] _buffer;

    private ISession _session;
    private bool _hasConnected;

    public ClientHandler()
    {
        _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        _buffer = new byte[8192];

        var timer = new Timer
        {
            Interval = 30000
        };

        timer.Elapsed += ElapsedTimer;
        timer.Enabled = true;
    }

    private void ElapsedTimer(object sender, ElapsedEventArgs e)
    {
        if (!CoreUtilities.IsConnected(_socket))
        {
            Connect();
        }
    }

    public void Connect()
    {
        var host = _session.ConfigHandler["server.host"];
        var port = short.Parse(_session.ConfigHandler["server.port"]);

        try
        {
            _socket.Connect(new IPEndPoint(IPAddress.Parse(host), port));
            _socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, OnIncomingData, _socket);

            CoreUtilities.LogToConsole("Successfully established a connection to the server.");
        }
        catch (SocketException)
        {
            CoreUtilities.LogToConsole("Failed to establish a connection with the server.");
        }
        finally
        {
            if (_socket.Connected &amp;&amp; CoreUtilities.IsConnected(_socket))
            {
                Send(new SocketPasswordComposer(_session.ConfigHandler["server.password"]));
            }
        }
    }
}


Here is the method I use to determine if the socket is fully connected to the server.

public static bool IsConnected(Socket socket)
{
    return !(socket.Poll(1, SelectMode.SelectRead) &amp;&amp; socket.Available == 0);
}


I feel that what I have coded above could be improved a lot.
I see there is problem in the usage to Timer. Consider the case if the server takes more time (> 5000) to connect, than the timer interval specified (3000) specified.  Then the timer elapsed event will fire again though the first elapsed event is not yet finished.  In order to avoid this stop the timer in the begin of Connect() and start the timer in end of Connect().