Internet connection resets after a while when connected to VPN

May 5, 2015 at 7:12 AM
Hi,

I am using ConnectionManager to connect to a VPN. Altough VPN connection is successful, after a while (usually less than 3 minutes) main connection(3G/HSDPA) drops, hence the VPN drops. If I connect to internet and VPN manually connection does not drop.

Any help would be appreciated.

I am using Windows Embedded/Mobile 6.x and Smart Device Framework DLL version is 2.3.12004.0 . Also both Internet and VPN are pre-configured prior to running code.

Also, I am doing this to keep connection alive (both Internet and VPN).

I am using the following code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net;
using OpenNETCF.Net;
using OpenNETCF.Net.NetworkInformation;

static void Main()
{
    while (true)
    {
        bool hasConnection = false;
        foreach (INetworkInterface ni in WirelessNetworkInterface.GetAllNetworkInterfaces())
        {
            //Is interface UP = Means running, turned on etc.
            if (ni.OperationalStatus.ToString() == "Up")
            {
                //Ignore Loopback(169.254.2.1) and empty(0.0.0.0) IP addresses
                if ((ni.CurrentIpAddress.ToString() != "0.0.0.0") && (ni.CurrentIpAddress.ToString() != "169.254.2.1"))
                {
                    Log.WriteLogFile("IP: " + ni.CurrentIpAddress.ToString());
                    hasConnection = true;
                }
            }
        }
        if (hasConnection == false)
        {
            string address = "http://www.google.com";
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(address);
                WebResponse response = req.GetResponse();
                ReConnectVPN();
            }
            catch (Exception ex)
            {
                Log.WriteLogFile(ex.ToString());
            }
        }
        else
        {
            ReConnectVPN();
        }

        System.Threading.Thread.Sleep(10 * 1000);
    }
}

private static void ReConnectVPN()
{
    Log.WriteLogFile("1 - Entered ReConnectVPN()");
    try
    {
        ConnectionManager connectionManager = new ConnectionManager();
        ConnectionDetailCollection connections = connectionManager.GetConnectionDetailItems();
        ConnectionDetail connection = null;
        try
        {
            bool isVPNConnected = false;
            foreach (ConnectionDetail cd in connections)
            {
                if (cd.ConnectionType == ConnectionType.VPN)
                {
                    connection = cd;
                    Log.WriteLogFile("VPN Status:" + cd.ConnectionStatus.ToString());
                    if (cd.ConnectionStatus == ConnectionStatus.Connected || cd.ConnectionStatus == ConnectionStatus.WaitingConnection )
                    {
                        Log.WriteLogFile("2 - VPN is already connected");
                        isVPNConnected = true;
                        Log.WriteLogFile("5 - Leaving ReConnectVPN()");
                        return;
                    }
                    break;
                }
            }
            if (connection == null)
            {
                Log.WriteLogFile("3 - No VPN network configured");
                Log.WriteLogFile("5 - Leaving ReConnectVPN()");
                return;
            }
            //Log.WriteLogFile("Connecting to VPN");
            if (isVPNConnected == false)
            {
                Log.WriteLogFile("4 - No Active VPN connection, Trying to connect...");
                connectionManager.Connect(connection.DestinatonNetwork, true, ConnectionMode.Asynchronous);
            }

        }
        catch (Exception ex)
        {
            Log.WriteLogFile("Error ReConnectingVPN(): " + ex.Message);
        }

        connection = null;
        connectionManager = null;
    }
    catch (Exception ex)
    {
        Log.WriteLogFile("Error getting VPN connections: " + ex.Message);
    }
    Log.WriteLogFile("5 - Leaving ReConnectVPN()");
}