I am not sure if this will help.. but lets see...
when u want to dock the window... u are moving TOWARDS the edge... and when u want to undock, you are moveing AWAY from the edge.
So while moving, you can make a check if the window is moving away or towards the edge, and dock / undock the window accrodingly