Can I take plan B before sex?

Victoria
Will plan B still work if I take it just before having sex instead of the morning after?