You can cancel anytime. The consequences for canceling will be in your contract. If you are using the standard forms provided by NWMLS, your earnest money may be at risk, but would also be the most you are at risk.
You may have several legitimate options to cancel however, if you are in the proper time frames. This could include your inspection contingency, the seller's disclosure, title or neighborhood review just to name a few. The timeframes attached to these are in your contract.
Before you hire a lawyer, start with your agent. If you used the listing agent, you may want to review the contract carefully yourself first, the listing agent represents the seller first and foremost. They owe you a duty of professionalism, but not of full representation.