STATUS_INVALID_DEVICE_STATE with TDI call

Hello All,
I seem to be getting a STATUS_INVALID_DEVICE_STATE whenever
I try to send on a connection. This status is returned by IoCallDriver
to the tcpip device object.
But It doesn’t occur all the time. Sometimes it just works fine.

The send/recv codes I used are from
http://www.acc.umu.se/~bosse/httpdisk-3.zip
and
http://www.rootkit.com/newsread.php?newsid=416

Both of them have this problem.

Does anyone know why the TCPIP returns this error?
How do I handle this error? Thanks.