How does it escape? You've reversed the directionVector, so I'd expect to come back!
Is it just alternating back and forth so fast that it skips the visible screen in both directions?
Leaving aside reversing, the max/min method will work to clamp a value. But that alone won't make it bounce back.
Your approach sounds about like what I'd do. You might try the following, so that the bounce-back starts at the wall (unless it ends up feeling MORE wrong):
if x > 400 then
x = 400
directionVector *= -1
end
(The *= -1 is just a shorthand way to do the same thing you already did.)