Windows 2003 -- RasHangUp blocks for several seconds before returning, and handle count keeps climbing?

I am using RAS to create and disconnect VPN connections.  I can establish the connection without any problems and transfer data across it.  However, I am experiencing two issues:

1.  RasDial blocks for about 5 seconds before returning.
2.  According to Task Manager, the program's handle count is steadily increasing, even though I am careful to give RAS enough time to release the handle.

Here is my hangup code, :

int hangup(HRASCONN* vpnConn)

   DWORD RASDialStatus = -1;
   RASCONNSTATUS vpnConnStatus;

   memset ((void*) &vpnConnStatus, 0, sizeof(RASCONNSTATUS));
   vpnConnStatus.dwSize = sizeof(RASCONNSTATUS);

   RASDialStatus = RasHangUp(*vpnConn);

   if (!RASDialStatus)
      while (RASDialStatus != ERROR_INVALID_HANDLE)
         RASDialStatus = RasGetConnectStatus (*vpnConn, &vpnConnStatus);

   /* Error handling stuff goes here */


I thought at first that the while() loop was causing the delay.  However, when I step through the code, RasHangup() blocks for about 5 seconds before it returns 0.  The while loop runs just once before the RasGetConnectStatus returns ERROR_INVALID_HANDLE.

Does anyone know what might be causing RasHangup to take so long to return?  Could it be something in the way that I am initializing the connection properties when I call RasDial?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I would not worry about the delay.   If it's troublesome you could always run that  code in a separate thread.

TomProAuthor Commented:
Unfortunately, the delay *is* an issue.  The program depends on retrieving data over VPN connections as quickly as possible.  It's important to have the user wait as little as possible as the program makes up to 20 consecutive VPN connections to locate information.  

Even cutting the delay down from 5 seconds to 3 seconds per connection could make considerable difference for our end users.
TomProAuthor Commented:
Does anyone have any additional insight on this issue?

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

You could spin off each connection as a separate thread, unless there is some concurrency issue.   With a little luck you could have everything happening in parallel instead of serially.

TomProAuthor Commented:
grg99, this approach is a good idea except for one problem:  the IP address that I access once I open the VPN is the same for each VPN connection.  

In other words, if I have VPN firewalls with external addresses a.a.a.a, b.b.b.b, and c.c.c.c, once I connect to each, my program interacts with a system at address e.e.e.e behind the VPN.

Also, I am still searching for an answer to why I am seeing an increase in handle count each time the posted code runs.

Thanks guys!
TomProAuthor Commented:
Update regarding this issue -- I have since switched to a different network connection strategy that avoids VPN, so I was unable to find a true fix for this problem.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.