I have VB.NET app that communicates with some external server (maintains login sessions via Intranet), and I want to listen for Sleep/Hibernate events such that when it happens, I want to logout an existing session system before computer goes to sleep, while my app will remain running in the background but won't do anything.

And vice versa, when computer is resumed from Hibernate or Woke up from sleep, I want to immediately login to the server.

How can I grab those events and execute my code? I believe this relates to Win32 API which I'm supposed to use in VB.

Thanks.