nsresult nsAutodial::DialDefault(const PRUnichar* hostName)
{
#ifdef WINCE_WINDOWS_MOBILE
  HANDLE connectionHandle;

  // always use http://
  nsString theURL;
  theURL.Append(L"http://");
  theURL.Append(hostName);
  
  GUID networkUID;
  ConnMgrMapURL(theURL.get(), &networkUID, 0);

  // Make the connection to the new network
  CONNMGR_CONNECTIONINFO conn_info;
  memset(&conn_info, 0, sizeof(conn_info));
  
  conn_info.cbSize      = sizeof(conn_info);
  conn_info.dwParams    = CONNMGR_PARAM_GUIDDESTNET;
  conn_info.dwPriority  = CONNMGR_PRIORITY_USERINTERACTIVE;
  conn_info.guidDestNet = networkUID;
  conn_info.bExclusive  = FALSE;
  conn_info.bDisabled   = FALSE;
  conn_info.dwFlags     = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | 
                          CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5 |
                          CONNMGR_FLAG_NO_ERROR_MSGS;

  DWORD status;
  HRESULT result = ConnMgrEstablishConnectionSync(&conn_info, 
                                                  &connectionHandle, 
                                                  60000,
                                                  &status);
  
  if (conn_info.guidDestNet != autodial_DestNetInternet &&
      (result != S_OK || status != CONNMGR_STATUS_CONNECTED)) {
    conn_info.guidDestNet = autodial_DestNetInternet;  
    result = ConnMgrEstablishConnectionSync(&conn_info, 
                                            &connectionHandle, 
                                            60000,
                                            &status);
  }
  
  if (result != S_OK || status != CONNMGR_STATUS_CONNECTED)
    return NS_ERROR_FAILURE;

  return NS_OK;
#else
  return NS_ERROR_FAILURE;
#endif
}
static nsresult DoPPCConnection()
{
    static HANDLE    gConnectionHandle = NULL;

    // Make the connection to the new network
    CONNMGR_CONNECTIONINFO conn_info;
    memset(&conn_info, 0, sizeof(CONNMGR_CONNECTIONINFO));

    conn_info.cbSize      = sizeof(CONNMGR_CONNECTIONINFO);
    conn_info.dwParams    = CONNMGR_PARAM_GUIDDESTNET;
    conn_info.dwPriority  = CONNMGR_PRIORITY_USERINTERACTIVE;
    conn_info.guidDestNet = IID_DestNetInternet;
    conn_info.bExclusive  = FALSE;
    conn_info.bDisabled   = FALSE;

    HANDLE tempConnectionHandle;
    DWORD status;
    HRESULT result = ConnMgrEstablishConnectionSync(&conn_info, 
                                                    &tempConnectionHandle, 
                                                    60000,
                                                    &status);

    if (result != S_OK)
    {
      return NS_ERROR_FAILURE;
    }

    if (status != CONNMGR_STATUS_CONNECTED)
    {
      // could not connect to this network.  release the
      // temp connection.
      ConnMgrReleaseConnection(tempConnectionHandle, 0);
      return NS_ERROR_FAILURE;
    }

    // At this point, we have a new connection, so release
    // the old connection
    if (gConnectionHandle)
      ConnMgrReleaseConnection(gConnectionHandle, 0);
      
    gConnectionHandle = tempConnectionHandle;
    return NS_OK;
}
HANDLE CBondiWidgetLibrary::ConnectToNetwork(int timeoutsecs)
{
	// handle to connection to start
	HANDLE hConnection = NULL;

#ifdef UNDER_CE
	// stores return value identifying status of connection attempt
	DWORD dwStatus;

	// initialise connection info structure
	CONNMGR_CONNECTIONINFO pConnectionInfo;
	ZeroMemory(&pConnectionInfo, sizeof(CONNMGR_CONNECTIONINFO));

	// set structure size
	pConnectionInfo.cbSize = sizeof(CONNMGR_CONNECTIONINFO);

	// set priority to identify that a user initiated this request
	// and the GUI is waiting on the creation of the connection
	pConnectionInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;

	// identify the network to connect to
	pConnectionInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
	pConnectionInfo.guidDestNet = IID_DestNetInternet;

	// specify that other applications can use this connection
	pConnectionInfo.bExclusive = FALSE;

	// specify that a connection should be made
	pConnectionInfo.bDisabled = FALSE;

	// request connection
	HRESULT hr = ConnMgrEstablishConnectionSync(&pConnectionInfo,
		&hConnection,
		timeoutsecs * 1000,
		&dwStatus);

	if (hr != S_OK)
	{
#ifdef _DEBUG
		switch (dwStatus)
		{
		case CONNMGR_STATUS_DISCONNECTED:
			MessageBox(NULL,TEXT("The connection has been disconnected"),TEXT("Connection Manager"),MB_ICONERROR);
			break;
		case CONNMGR_STATUS_WAITINGFORPATH:
			MessageBox(NULL,TEXT("A path to the destination exists but is not presently available"),TEXT("Connection Manager"),MB_ICONERROR);
			break;
		case CONNMGR_STATUS_WAITINGFORRESOURCE:
			MessageBox(NULL,TEXT("Another client is using resources that this connection requires"),TEXT("Connection Manager"),MB_ICONERROR);
			break;
		case CONNMGR_STATUS_WAITINGFORPHONE:
			MessageBox(NULL,TEXT("Connection cannot be made while call in progress"),TEXT("Connection Manager"),MB_ICONERROR);
			break;
		case CONNMGR_STATUS_NOPATHTODESTINATION:
			MessageBox(NULL,TEXT("No path to the destination could be found"),TEXT("Connection Manager"),MB_ICONERROR);
			break;
		case CONNMGR_STATUS_CONNECTIONFAILED:
			MessageBox(NULL,TEXT("The connection failed and cannot be reestablished"),TEXT("Connection Manager"),MB_ICONERROR);
			break;
		case CONNMGR_STATUS_CONNECTIONCANCELED:
			MessageBox(NULL,TEXT("The user aborted the connection"),TEXT("Connection Manager"),MB_ICONERROR);
			break;
		case CONNMGR_STATUS_WAITINGCONNECTION:
			MessageBox(NULL,TEXT("The device is attempting to connect"),TEXT("Connection Manager"),MB_ICONERROR);
			break;
		case CONNMGR_STATUS_WAITINGCONNECTIONABORT:
			MessageBox(NULL,TEXT("The device is aborting the connection attempt"),TEXT("Connection Manager"),MB_ICONERROR);
			break;
		case CONNMGR_STATUS_WAITINGDISCONNECTION:
			MessageBox(NULL,TEXT("The connection is being brought down"),TEXT("Connection Manager"),MB_ICONERROR);
			break;
		default:
			MessageBox(NULL,TEXT("The connection attempt failed"),TEXT("Connection Manager"),MB_ICONERROR);
			break;
		}
#endif
	}
#endif

	return hConnection;
}