Have you tested any of the Shutdown...() UDFs instead of using your homegrown code? Do they encounter the same problem?

Also, is a screensaver running on the computer that you try to shutdown, or is the computer locked by any chance?

finally, in order to shutdown a computer the user account being used needs to have the SeShutdown right which can be revoked in the 'Local Security Settings' screen. However, then at least my Shutdown UDF would give you a failiure code as it checks for the correct permission to shutdown a computer.
_________________________
There are two types of vessels, submarines and targets.